예제 #1
0
 /**
  * Wraps an event around the parent function
  *
  * @triggers HTTPCLIENT_REQUEST_SEND
  * @author   Andreas Gohr <*****@*****.**>
  */
 function sendRequest($url, $data = '', $method = 'GET')
 {
     $httpdata = array('url' => $url, 'data' => $data, 'method' => $method);
     $evt = new Doku_Event('HTTPCLIENT_REQUEST_SEND', $httpdata);
     if ($evt->advise_before()) {
         $url = $httpdata['url'];
         $data = $httpdata['data'];
         $method = $httpdata['method'];
     }
     $evt->advise_after();
     unset($evt);
     return parent::sendRequest($url, $data, $method);
 }
예제 #2
0
 /**
  * Retries sending the request multiple times
  *
  * @param string $url
  * @param string $data
  * @param string $method
  * @return bool
  */
 function sendRequest($url, $data = '', $method = 'GET')
 {
     $this->tries = 2;
     // configures the number of retries
     $return = false;
     while ($this->tries) {
         $return = parent::sendRequest($url, $data, $method);
         if ($this->status != -100) {
             break;
         }
         $this->tries--;
     }
     return $return;
 }
예제 #3
0
function macro_ImageFileSize($formatter, $value = '', $params = array())
{
    global $Config;
    if (empty($value)) {
        return '';
    }
    $sz = 0;
    // check if it is valid or not
    while (preg_match('/^((?:https?|ftp):\\/\\/.*(\\.(?:jpg|jpeg|gif|png)))(?:\\?|&)?/i', $value, $m)) {
        $value = $m[1];
        // check the file size saved by the fetch plugin
        $si = new Cache_text('fetchinfo');
        if ($si->exists($value) && ($info = $si->fetch($value)) !== false) {
            $sz = $info['size'];
            break;
        }
        $sc = new Cache_text('imagefilesize');
        if (empty($params['.refresh']) and $sc->exists($value) and $sc->mtime($value) < time() + 60 * 60 * 24 * 20) {
            $sz = $sc->fetch($value);
        } else {
            // dynamic macro
            if ($formatter->_macrocache and empty($params['call'])) {
                return $formatter->macro_cache_repl('ImageFileSize', $value);
            }
            if (empty($params['call'])) {
                $formatter->_dynamic_macros['@ImageFileSize'] = 1;
            }
            // do not fetch the size of image right now. just fetch the cached info by the fetch plugin
            if (empty($params['call']) and !empty($Config['fetch_imagesize']) and $Config['fetch_imagesize'] == 2) {
                return _("Unknown");
            }
            require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
            $http = new HTTPClient();
            // support proxy
            if (!empty($Config['proxy_host'])) {
                $http->proxy_host = $Config['proxy_host'];
                if (!empty($Config['proxy_port'])) {
                    $http->proxy_port = $Config['proxy_port'];
                }
            }
            // set referrer
            $referer = '';
            if (!empty($Config['fetch_referer_re'])) {
                foreach ($Config['fetch_referer_re'] as $re => $ref) {
                    if (preg_match($re, $value)) {
                        $referer = $ref;
                        break;
                    }
                }
            }
            // default referrer
            if (empty($referer) and !empty($Config['fetch_referer'])) {
                $referer = $Config['fetch_referer'];
            }
            if (empty($referer)) {
                $referer = qualifiedUrl($formatter->link_url($formatter->page->urlname));
            }
            $http->nobody = true;
            $http->referer = $referer;
            // check HEAD support for the internal fetch plugin
            $method = 'GET';
            if (!empty($Config['fetch_imagesize_head']) && strstr($value, $Config['fetch_action'])) {
                $method = 'HEAD';
            }
            $http->sendRequest($value, array(), $method);
            if ($http->status != 200) {
                return _("Unknown");
            }
            if (isset($http->resp_headers['content-length'])) {
                $sz = $http->resp_headers['content-length'];
            }
            $sc->update($value, $sz);
        }
        break;
    }
    if ($sz > 0) {
        $unit = array('Bytes', 'KB', 'MB', 'GB');
        for ($i = 0; $i < 4; $i++) {
            if ($sz <= 1024) {
                break;
            }
            $sz = $sz / 1024;
        }
        return round($sz, 2) . ' ' . $unit[$i];
    }
    // set as dynamic macro or not.
    if ($formatter->_macrocache and empty($options['call'])) {
        return $formatter->macro_cache_repl('ImageFileSize', $value);
    }
    if (empty($options['call'])) {
        $formatter->_dynamic_macros['@ImageFileSize'] = 1;
    }
    return _("Unknown");
}
예제 #4
0
 function getPages($params)
 {
     global $DBInfo;
     $http = new HTTPClient();
     if (empty($this->json)) {
         $this->json = new Services_JSON();
     }
     $offset = 0;
     if (!empty($params['offset']) and is_numeric($params['offset']) and $params['offset'] > 0) {
         $offset = $params['offset'];
     }
     // set page_limit
     $pages_limit = isset($DBInfo->pages_limit) ? $DBInfo->pages_limit : 5000;
     // 5000 pages
     $total = $this->pageCount();
     $size = $pages_limit;
     if (!empty($params['all'])) {
         $size = $total;
     }
     $url = $this->index_url . '/text/_search';
     $data = '{ "fields" : "title", "from" : ' . $offset . ', "size" : ' . $size . ', "query": { "query_string" : {"query" : "*"} } }';
     if (!$http->sendRequest($url, $data, 'POST')) {
         return false;
     }
     if ($http->status != 200) {
         return false;
     }
     if ($http->resp_body === false) {
         return false;
     }
     $pages = array();
     $json = $this->json->decode($http->resp_body);
     foreach ($json->hits->hits as $hit) {
         $pages[] = $hit->_id;
     }
     $info['offset'] = $offset;
     $info['count'] = count($pages);
     if (isset($params['ret'])) {
         $params['ret'] = $info;
     }
     return $pages;
 }
예제 #5
0
function macro_WikimediaCommons($formatter, $value, $params = array())
{
    global $DBInfo, $Config;
    $args = array();
    if (($p = strpos($value, ',')) !== false) {
        $arg = substr($value, $p + 1);
        $value = substr($value, 0, $p);
        $arg = preg_replace('@\\s*,\\s*@', ',', $arg);
        $arg = preg_replace('@\\s*=\\s*@', '=', $arg);
        $args = explode(',', $arg);
    }
    if (!empty($params['attr'])) {
        $args = array_merge($args, (array) $params['attr']);
    }
    $data = array('action' => 'query', 'prop' => 'imageinfo', 'iiprop' => 'extmetadata|url', 'format' => 'json', 'rawcontinue' => '1');
    // default API url
    $api_url = 'https://commons.wikimedia.org/w/api.php';
    // check full url
    if (preg_match('@^https?://upload\\.wikimedia\\.org/wikipedia/(?:(en|commons)/)?(thumb/)?./../([^/]+\\.(?:gif|jpe?g|png|svg))(?(2)/(\\d+px)-\\3)@', $value, $m)) {
        // WikiMedia
        $remain = substr($value, strlen($m[0]));
        if (!empty($m[1]) && in_array($m[1], array('en'))) {
            $api_url = 'https://' . $m[1] . '.wikipedia.org/w/api.php';
        }
        $value = urldecode($m[3]);
        if (!empty($m[4])) {
            $width = intval($m[4]);
        }
        $data['titles'] = 'Image:' . $value;
        $data['iiprop'] = 'extmetadata|url';
    } else {
        if (preg_match('@^https?://((?:[^.]+)\\.(?:wikimedia|wikipedia)\\.org)/wiki/(?:Image|File):([^/]+\\.(?:gif|jpe?g|png|svg))$@', $value, $m)) {
            // WikiMedia or WikiPedia
            $api_url = 'https://' . $m[1] . '/w/api.php';
            $value = urldecode($m[2]);
            $data['titles'] = 'Image:' . $value;
            $data['iiprop'] = 'extmetadata|url';
            $source = _("WikiMedia Commons");
        } else {
            if (preg_match('@^https?://([^.]+)\\.wikia\\.com/wiki/(?:Image|File):(.*\\.(?:gif|jpe?g|png|svg))@', $value, $m)) {
                $src = 'wikia.';
                // Wikia
                $api_url = 'https://' . $m[1] . '.wikia.com/api.php';
                $value = urldecode($m[2]);
                $data['titles'] = 'Image:' . $value;
                $data['iiprop'] = 'url|user|size|comment';
                $source = _("Wikia");
            } else {
                if (preg_match('@^https?://.*\\.wikia\\..*/([^/]+)/images/./../([^/]+\\.(?:gif|jpe?g|png|svg))@', $value, $m)) {
                    $src = 'wikia.';
                    // Wikia
                    $api_url = 'https://' . $m[1] . '.wikia.com/api.php';
                    $value = urldecode($m[2]);
                    $data['titles'] = 'Image:' . $value;
                    $data['iiprop'] = 'url|user|size|comment';
                    $source = _("Wikia");
                } else {
                    $value = urldecode($value);
                    $data['titles'] = 'Image:' . $value;
                    $data['iiprop'] = 'extmetadata|url';
                    $source = _("WikiMedia Commons");
                }
            }
        }
    }
    $styles = array();
    foreach ($args as $arg) {
        $k = $v = '';
        if (($p = strpos($arg, '=')) !== false) {
            $k = substr($arg, 0, $p);
            $k = trim($k);
            $v = substr($arg, $p + 1);
            $v = trim($v, '"\'');
        } else {
            continue;
        }
        $k = strtolower($k);
        switch ($k) {
            case 'width':
            case 'height':
                if (preg_match('@^(\\d+)(px|%)?$@', $v, $m)) {
                    if (isset($m[2]) && $m[2] == '%') {
                        $styles[$k] = $v;
                    } else {
                        $styles[$k] = $v;
                        ${$k} = intval($m[1]);
                    }
                }
                break;
            case 'align':
                $v = strtolower($v);
                if (in_array($v, array('left', 'right', 'center'))) {
                    $addClass = ' img' . ucfirst($v);
                }
                break;
        }
    }
    $common = new Cache_Text('wikicommons');
    $key = $value . $src;
    if (isset($width)) {
        $key .= $width;
    }
    if (isset($height)) {
        $key .= '.h' . $height;
    }
    if (!empty($formatter->refresh) || ($images = $common->fetch($key)) === false) {
        if (!empty($width)) {
            $data['iiurlwidth'] = min(1280, $width);
        } else {
            if (!empty($height)) {
                $data['iiurlheight'] = min(1280, $height);
            } else {
                // default image width
                $data['iiurlwidth'] = 640;
            }
        }
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $http = new HTTPClient();
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        // support proxy
        if (!empty($DBInfo->proxy_host)) {
            $http->proxy_host = $DBInfo->proxy_host;
            if (!empty($DBInfo->proxy_port)) {
                $http->proxy_port = $DBInfo->proxy_port;
            }
        }
        $http->sendRequest($api_url, $data, 'POST');
        set_time_limit($save);
        // FIXME
        if ($http->status != 200) {
            return '';
        }
        $res = json_decode($http->resp_body);
        $images = $res->query->pages;
        $common->update($key, $images);
    }
    $image = current($images);
    $image_url = $image->imageinfo[0]->thumburl;
    $desc_url = $image->imageinfo[0]->descriptionurl;
    if (empty($styles['width']) && !empty($image->imageinfo[0]->thumbwidth)) {
        $styles['width'] = $image->imageinfo[0]->thumbwidth . 'px';
    }
    $attrs = array();
    $keys = array('width', 'height');
    foreach ($keys as $key) {
        if (isset($styles[$key])) {
            $attrs[] = $key . '="' . $styles[$key] . '"';
            unset($styles[$key]);
        }
    }
    $attr = '';
    if (count($attrs)) {
        $attr = ' ' . implode(' ', $attrs);
    }
    $style = '';
    foreach ($styles as $k => $v) {
        $style .= $k . ':' . $v . ';';
    }
    if (!empty($style)) {
        $style = ' style="' . $style . '"';
    }
    if (!empty($image->imageinfo[0]->extmetadata)) {
        $copyright = $image->imageinfo[0]->extmetadata->Copyrighted->value;
        $description = $image->imageinfo[0]->extmetadata->ImageDescription->value;
        $author = $image->imageinfo[0]->extmetadata->Artist->value;
        $license = $image->imageinfo[0]->extmetadata->License->value;
        $comment = '';
    } else {
        if (!empty($image->imageinfo[0]->user)) {
            // Wikia case
            $copyright = 'True';
            $author = sprintf(_("Uploaded by %s"), $image->imageinfo[0]->user);
            $license = '';
            $description = $image->imageinfo[0]->comment;
        } else {
            // not found
            return false;
        }
    }
    if (!empty($formatter->fetch_images) && !empty($image_url)) {
        $image_url = $formatter->fetch_action . str_replace(array('&', '?'), array('%26', '%3f'), $image_url);
        // use thumbnails ?
        if (!empty($formatter->use_thumb_by_default)) {
            $image_url .= '&amp;thumbwidth=' . $formatter->thumb_width;
        }
    }
    $copyrighted = $copyright == 'True';
    $info = ($copyrighted ? '&copy; ' : '(&#596;) ') . $author;
    if ($copyrighted && isset($license[0])) {
        $info .= " ({$license})";
    }
    $out = '<div class="externalImage">';
    if (empty($addClass)) {
        $cls = ' class="' . $addClass . '"';
    }
    $out .= "<div" . $cls . "><img src='{$image_url}'{$style}{$attr}>";
    $out .= "<div class='info'>" . $info . $comment . ' from ' . "<a href='{$desc_url}' target='_blank'>{$source}</a></div>";
    $out .= "</div>";
    if (!empty($DBInfo->wikimediacommons_use_description) && !empty($description)) {
        $out .= '<div class="desc">' . $description . '</div>';
    }
    $out .= "</div>\n";
    return $out;
}
예제 #6
0
function macro_Fetch($formatter, $url = '', $params = array())
{
    global $DBInfo;
    if (empty($url)) {
        $params['retval']['error'] = _("Empty URL");
        return false;
    }
    // check valid url
    if (!preg_match('@^((ftp|https?)://[^/]+)/@', $url, $m)) {
        return false;
    }
    $siteurl = $m[1];
    require_once "lib/HTTPClient.php";
    $sz = 0;
    $allowed = 'png|jpeg|jpg|gif';
    if (!empty($DBInfo->fetch_exts)) {
        $allowed = $DBInfo->fetch_exts;
    }
    // urlencode()
    $url = _urlencode($url);
    // set default params
    $maxage = !empty($DBInfo->fetch_maxage) ? (int) $DBInfo->fetch_maxage : 60 * 60 * 24 * 7;
    $timeout = !empty($DBInfo->fetch_timeout) ? (int) $DBInfo->fetch_timeout : 15;
    $vartmp_dir = $DBInfo->vartmp_dir;
    $buffer_size = 2048 * 1024;
    // default buffer size
    if (!empty($DBInfo->fetch_buffer_size) and $DBInfo->fetch_buffer_size > 2048 * 1024) {
        $buffer_size = $DBInfo->fetch_buffer_size;
    }
    // set referrer
    $referer = '';
    if (!empty($DBInfo->fetch_referer_re)) {
        foreach ($DBInfo->fetch_referer_re as $re => $ref) {
            if (preg_match($re, $url)) {
                $referer = $ref;
                break;
            }
        }
    }
    // default referrer
    if (empty($referer) and !empty($DBInfo->fetch_referer)) {
        $referer = $DBInfo->fetch_referer;
    }
    // check site available
    $si = new Cache_text('siteinfo');
    if ($si->exists($siteurl)) {
        if (!empty($params['refresh'])) {
            $si->remove($siteurl);
        } else {
            if (empty($params['refresh']) && ($check = $si->fetch($siteurl)) !== false) {
                $params['retval']['status'] = $check['status'];
                $params['retval']['error'] = $check['error'];
                return false;
            }
        }
    }
    $sc = new Cache_text('fetchinfo');
    $error = null;
    if (empty($params['refresh']) and $sc->exists($url) and $sc->mtime($url) < time() + $maxage) {
        $info = $sc->fetch($url);
        $sz = $info['size'];
        $mimetype = $info['mimetype'];
        $error = !empty($info['error']) ? $info['error'] : null;
        // already retrived and found some error
        if (empty($params['refresh']) and !empty($error)) {
            $params['retval']['status'] = $info['status'];
            $params['retval']['error'] = $error;
            $params['retval']['mimetype'] = $mimetype;
            $params['retval']['size'] = $sz;
            return false;
        }
    } else {
        // check connection
        $http = new HTTPClient();
        // get file header
        $http->nobody = true;
        $http->referer = $referer;
        $http->sendRequest($url, array(), 'GET');
        //if ($http->status == 301 || $http->status == 302 ) {
        //
        //}
        if ($http->status != 200) {
            if ($http->status == 404) {
                $params['retval']['error'] = '404 File Not Found';
            } else {
                $params['retval']['error'] = !empty($http->error) ? $http->error : sprintf(_("Invalid Status %d"), $http->status);
            }
            $params['retval']['status'] = $http->status;
            // check alive site
            if ($http->status == -210) {
                $si->update($siteurl, array('status' => $http->status, 'error' => $params['retval']['error']), 60 * 60 * 24);
                return false;
            }
            $sc->update($url, array('size' => -1, 'mimetype' => '', 'error' => $params['retval']['error'], 'status' => $params['retval']['status']), 60 * 60 * 24 * 3);
            return false;
        }
        if (isset($http->resp_headers['content-length'])) {
            $sz = $http->resp_headers['content-length'];
        }
        if (isset($http->resp_headers['content-type'])) {
            $mimetype = $http->resp_headers['content-type'];
        } else {
            $mimetype = 'application/octet-stream';
        }
        $sc->update($url, array('size' => $sz, 'mimetype' => $mimetype));
    }
    // size info
    if (is_numeric($sz)) {
        $unit = array('Bytes', 'KB', 'MB', 'GB');
        $tmp = $sz;
        for ($i = 0; $i < 4; $i++) {
            if ($tmp <= 1024) {
                break;
            }
            $tmp = $tmp / 1024;
        }
        $hsz = round($tmp, 2) . ' ' . $unit[$i];
        if (empty($buffer_size) && !empty($DBInfo->fetch_max_size) and $sz > $DBInfo->fetch_max_size) {
            $params['retval']['error'] = sprintf(_("Too big file size (%s). Please contact WikiMasters to increase \$fetch_max_size"), $hsz);
            $params['retval']['mimetype'] = $mimetype;
            return false;
        }
    } else {
        $params['retval']['error'] = _("Can't get file size info");
        $params['retval']['mimetype'] = $mimetype;
        return false;
    }
    $is_image = false;
    if (preg_match('/^image\\/(jpe?g|gif|png)$/', $mimetype, $m)) {
        $ext = isset($m[1]) ? '.' . $m[1] : '';
        $is_image = true;
    } else {
        $ext = '.' . get_extension('data/mime.types', $mimetype);
    }
    if (!empty($DBInfo->fetch_images_only) and !$is_image) {
        // always check the content-type
        $params['retval']['error'] = sprintf(_("Invalid mime-type %s"), $mimetype);
        $params['retval']['mimetype'] = $mimetype;
        return false;
    }
    if (empty($params['call'])) {
        if ($is_image) {
            $img_url = $formatter->link_url('', '?action=fetch&amp;url=' . $url);
            return '<div class="externalImage"><div><img src="' . $img_url . '">' . '<div><a href="' . $url . '"><span>[' . strtoupper($m[1]) . ' ' . _("external image") . ' (' . $hsz . ')' . ']</span></a></div></div>';
        }
        return $formatter->link_to('?action=fetch&amp;url=' . $url, $mimetype . ' (' . $hsz . ')');
    }
    // cache dir/filename/cache url
    if (!empty($DBInfo->cache_public_dir) and !empty($DBInfo->cache_public_url)) {
        $fc = new Cache_text('fetchfile', array('dir' => $DBInfo->cache_public_dir));
        $fetchname = $fc->getKey($url);
        $fetchfile = $DBInfo->cache_public_dir . '/' . $fetchname . $ext;
        $fetch_url = $DBInfo->cache_public_url . '/' . $fetchname . $ext;
    } else {
        $fc = new Cache_text('fetchfile');
        $fetchname = $fc->getKey($url);
        $fetchfile = $fc->cache_dir . '/' . $fetchname;
        $fetch_url = null;
    }
    // real fetch job.
    if (!empty($params['refresh']) or !file_exists($fetchfile)) {
        $fp = fopen($fetchfile, 'w');
        if (!is_resource($fp)) {
            $params['retval']['error'] = sprintf(_("Fail to open %s"), $fetchfile);
            return false;
        }
        // retry to get all info
        $http = new HTTPClient();
        if (!empty($buffer_size)) {
            $http->max_buffer_size = $buffer_size;
        }
        $http->vartmp_dir = $vartmp_dir;
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http->timeout = $timeout;
        $http->referer = $referer;
        $http->sendRequest($url, array(), 'GET');
        set_time_limit($save);
        if ($http->status != 200) {
            fclose($fp);
            unlink($fetchfile);
            // Error found! save error status to the info cache
            $params['retval']['status'] = $http->status;
            $params['retval']['error'] = $http->error;
            $params['retval']['mimetype'] = $mimetype;
            $params['retval']['size'] = $sz;
            $sc->update($url, array('size' => $sz, 'mimetype' => $mimetype, 'error' => $http->error, 'status' => $params['retval']['status']));
            return false;
        }
        if (!empty($http->resp_body)) {
            fwrite($fp, $http->resp_body);
            fclose($fp);
        } else {
            fclose($fp);
            if (!empty($http->resp_body_file) && file_exists($http->resp_body_file)) {
                copy($http->resp_body_file, $fetchfile);
                unlink($http->resp_body_file);
            }
        }
        // update error status.
        if (!empty($error)) {
            $sc->update($url, array('size' => $sz, 'mimetype' => $mimetype));
        }
    }
    if (!empty($fetch_url) and !empty($DBInfo->fetch_use_cache_url)) {
        $formatter->send_header(array('Status: 302', 'Location: ' . $fetch_url));
        return null;
    }
    if (!empty($params['thumbwidth'])) {
        // check allowed thumb widths.
        $thumb_widths = isset($DBInfo->thumb_widths) ? $DBInfo->thumb_widths : array('120', '240', '320', '480', '600', '800', '1024');
        $width = 320;
        // default
        if (!empty($DBInfo->default_thumb_width)) {
            $width = $DBInfo->default_thumb_width;
        }
        if (!empty($thumb_widths)) {
            if (in_array($params['thumbwidth'], $thumb_widths)) {
                $width = $params['thumbwidth'];
            } else {
                header("HTTP/1.1 404 Not Found");
                echo "Invalid thumbnail width", "<br />", "valid thumb widths are ", implode(', ', $thumb_widths);
                return;
            }
        } else {
            $width = $params['thumbwidth'];
        }
        $thumb_width = $width;
        $force_thumb = true;
    } else {
        // automatically generate thumb images to support low-bandwidth mobile version
        if (is_mobile()) {
            $force_thumb = (!isset($params['m']) or $params['m'] == 1);
        }
    }
    // generate thumb file to support low-bandwidth mobile version
    $thumbfile = '';
    while ((!empty($params['thumb']) or $force_thumb) and preg_match('/^image\\/(jpe?g|gif|png)$/', $mimetype)) {
        if (empty($thumb_width)) {
            $thumb_width = 320;
            // default
            if (!empty($DBInfo->fetch_thumb_width)) {
                $thumb_width = $DBInfo->fetch_thumb_width;
            }
        }
        $thumbfile = preg_replace('@' . $ext . '$@', '.w' . $thumb_width . $ext, $fetchfile);
        if (empty($params['refresh']) && file_exists($thumbfile)) {
            break;
        }
        list($w, $h) = getimagesize($fetchfile);
        if ($w <= $thumb_width) {
            $thumbfile = $fetchfile;
            break;
        }
        require_once 'lib/mediautils.php';
        // generate thumbnail using the gd func or the ImageMagick(convert)
        resize_image($ext, $fetchfile, $thumbfile, $w, $h, $thumb_width);
        break;
    }
    $down_mode = 'inline';
    if (!empty($thumbfile)) {
        $fetchfile = $thumbfile;
    }
    header("Content-Type: {$mimetype}\r\n");
    header("Content-Length: " . filesize($fetchfile));
    //header("Content-Disposition: $down_mode; ".$fname );
    header("Content-Description: MoniWiki PHP Fetch Downloader");
    $mtime = filemtime($fetchfile);
    $lastmod = gmdate("D, d M Y H:i:s", $mtime) . ' GMT';
    $etag = md5($lastmod . $url . $thumbfile);
    header("Last-Modified: " . $lastmod);
    header('ETag: "' . $etag . '"');
    header("Pragma:");
    header('Cache-Control: public, max-age=' . $maxage);
    $need = http_need_cond_request($mtime, $lastmod, $etag);
    if (!$need) {
        header('X-Cache-Debug: Cached OK');
        header('HTTP/1.0 304 Not Modified');
        @ob_end_clean();
        return null;
    }
    @ob_clean();
    $ret = readfile($fetchfile);
    return null;
}
예제 #7
0
function do_wikidiff($formatter, $params = array())
{
    global $Config;
    $supported = array('default' => '%0%2?action=raw', 'namuwiki' => '%1raw/%2');
    if (!empty($Config['wikidiff_sites'])) {
        $wikis = $Config['wikidiff_sites'];
    } else {
        $wikis = array('kowikipedia' => 'https://ko.wikipedia.org/wiki/', 'librewiki' => 'http://librewiki.net/wiki/', 'namuwiki' => 'https://namu.wiki/raw/');
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['wiki']) && isset($wikis[$_POST['wiki']])) {
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $wiki = $_POST['wiki'];
        if (isset($supported[$wiki])) {
            $format_url = $supported[$wiki];
        } else {
            $format_url = $supported['default'];
        }
        $url = $wikis[$wiki];
        $parsed = parse_url($url);
        if (isset($_POST['value'][0])) {
            $pagename = rawurlencode($_POST['value']);
        } else {
            $pagename = $formatter->page->urlname;
        }
        // translate table.
        $trs = array('%0' => $url, '%1' => $parsed['scheme'] . '://' . $parsed['host'] . '/', '%2' => $pagename);
        $request_url = strtr($format_url, $trs);
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http = new HTTPClient();
        $http->timeout = 15;
        // set timeout
        // support proxy
        if (!empty($Config['proxy_host'])) {
            $http->proxy_host = $Config['proxy_host'];
            if (!empty($Config['proxy_port'])) {
                $http->proxy_port = $Config['proxy_port'];
            }
        }
        $http->sendRequest($request_url, array(), 'GET');
        set_time_limit($save);
        $formatter->send_header('', $params);
        if ($http->status != 200) {
            $params['.title'] = sprintf(_("Fail to connect %s"), $http->status);
            $diff = null;
        } else {
            $diff = $formatter->get_diff($http->resp_body);
            $params['.title'] = sprintf(_("Difference between this wiki and %s."), $wiki);
        }
        $formatter->send_title('', '', $params);
        if (isset($diff[0])) {
            echo "<div id='wikiDiffPreview'>\n";
            echo $formatter->processor_repl('diff', $diff, $params);
            echo "</div>\n";
        } else {
            if ($http->status != 200) {
                echo sprintf(_("Status: %s"), $http->status);
            } else {
                echo _("No difference found.");
            }
        }
        $formatter->send_footer('', $params);
        return;
    }
    $select = '<select name="wiki">';
    $select .= '<option>' . _("-- Select Wiki --") . '</option>';
    foreach ($wikis as $w => $url) {
        $select .= '<option value="' . $w . '">' . $w . '</option>' . "\n";
    }
    $select .= '</select>';
    $name = isset($_GET['value'][0]) ? $_GET['value'] : '';
    $default = _html_escape($formatter->page->name);
    $optional = '<br />' . _("Page name:") . ' <input type="text" name="value" placeholder="' . $default . '" value="' . _html_escape($name) . '" /><br />';
    //$optional .= _("Reverse order:")." <input type='checkbox' name='reverse' /> ";
    $params['.title'] = _("Show difference between wikis.");
    $button = _("Diff");
    $formatter->send_header('', $params);
    $formatter->send_title('', '', $params);
    echo <<<FORM
<form method='post'>
{$select}
{$optional}
<input type='submit' value='{$button}' />
<input type='hidden' name='action' value='wikidiff' />
</form>
FORM;
    $formatter->send_footer('', $params);
    return;
}
예제 #8
0
파일: Play.php 프로젝트: ahastudio/moniwiki
function macro_Play($formatter, $value, $params = array())
{
    global $DBInfo;
    static $autoplay = 1;
    $max_width = 600;
    $max_height = 400;
    $default_width = 320;
    $default_height = 240;
    // get the macro alias name
    $macro = 'play';
    if (!empty($params['macro_name']) and $params['macro_name'] != 'play') {
        $macro = $params['macro_name'];
    }
    // use alias macro name as [[Youtube()]], [[Vimeo()]]
    #
    $media = array();
    #
    preg_match("/^(([^,]+\\s*,?\\s*)+)\$/", $value, $match);
    if (!$match) {
        return '[[Play(error!! ' . $value . ')]]';
    }
    $align = '';
    // parse arguments height, width, align
    if (($p = strpos($match[1], ',')) !== false) {
        $my = explode(',', $match[1]);
        $my = array_map('trim', $my);
        for ($i = 0, $sz = count($my); $i < $sz; $i++) {
            if (strpos($my[$i], '=')) {
                list($key, $val) = explode('=', $my[$i], 2);
                $val = trim($val, '"\'');
                $val = trim($val);
                if ($key == 'width' and $val > 1) {
                    $width = intval($val);
                } else {
                    if ($key == 'height' and $val > 1) {
                        $height = intval($val);
                    } else {
                        if ($key == 'align') {
                            if (in_array($val, array('left', 'center', 'right'))) {
                                $align = ' obj' . ucfirst($val);
                            }
                        } else {
                            $media[] = $my[$i];
                        }
                    }
                }
            } else {
                // multiple files
                $media[] = $my[$i];
            }
        }
    } else {
        $media[] = trim($match[1]);
    }
    # set embeded object size
    $mywidth = !empty($width) ? min($width, $max_width) : null;
    $myheight = !empty($height) ? min($height, $max_height) : null;
    $width = !empty($width) ? min($width, $max_width) : $default_width;
    $height = !empty($height) ? min($height, $max_height) : $default_height;
    $url = array();
    $my_check = 1;
    for ($i = 0, $sz = count($media); $i < $sz; $i++) {
        if (!preg_match("/^((https?|ftp|mms|rtsp):)?\\/\\//", $media[$i])) {
            if ($macro != 'play') {
                // will be parsed later
                $url[] = $media[$i];
                continue;
            }
            $fname = $formatter->macro_repl('Attachment', $media[$i], array('link' => 1));
            if ($my_check and !file_exists($fname)) {
                return $formatter->macro_repl('Attachment', $value);
            }
            $my_check = 1;
            // check only first file.
            $fname = str_replace($DBInfo->upload_dir, $DBInfo->upload_dir_url, $fname);
            $url[] = qualifiedUrl(_urlencode($fname));
        } else {
            $url[] = $media[$i];
        }
    }
    if ($autoplay == 1) {
        $play = "true";
    } else {
        $play = "false";
    }
    #
    $use_flashplayer_ok = 0;
    if ($DBInfo->use_jwmediaplayer) {
        $use_flashplayer_ok = 1;
        for ($i = 0, $sz = count($media); $i < $sz; $i++) {
            // check type of all files
            if (!preg_match("/(flv|mp3|mp4|swf)\$/i", $media[$i])) {
                $use_flashplayer_ok = 0;
                break;
            }
        }
    }
    if ($use_flashplayer_ok) {
        # set embed flash size
        if (($sz = count($media)) == 1 and preg_match("/(ogg|wav|mp3)\$/i", $media[0])) {
            // only one and a sound file
            $height = 20;
            // override the hegiht of the JW MediaPlayer
        }
        $swfobject_num = !empty($GLOBALS['swfobject_num']) ? $GLOBALS['swfobject_num'] : 0;
        $swfobject_script = '';
        if (!$swfobject_num) {
            $swfobject_script = "<script type=\"text/javascript\" src=\"{$DBInfo->url_prefix}/local/js/swfobject.js\"></script>\n";
            $num = 1;
        } else {
            $num = ++$swfobject_num;
        }
        $GLOBALS['swfobject_num'] = $num;
        if (!$DBInfo->jwmediaplayer_prefix) {
            $_swf_prefix = qualifiedUrl("{$DBInfo->url_prefix}/local/JWPlayers");
            // FIXME
        } else {
            $_swf_prefix = $DBInfo->jwmediaplayer_prefix;
        }
        $addparam = '';
        if ($sz > 1) {
            $md5sum = md5(implode(':', $media));
            if ($DBInfo->cache_public_dir) {
                $fc = new Cache_text('jwmediaplayer', array('dir' => $DBInfo->cache_public_dir));
                $fname = $fc->getKey($md5sum, false);
                $basename = $DBInfo->cache_public_dir . '/' . $fname;
                $urlbase = $DBInfo->cache_public_url ? $DBInfo->cache_public_url . '/' . $fname : $DBInfo->url_prefix . '/' . $basename;
                $playfile = $basename . '.xml';
            } else {
                $cache_dir = $DBInfo->upload_dir . "/VisualTour";
                $cache_url = $DBInfo->upload_url ? $DBInfo->upload_url . '/VisualTour' : $DBInfo->url_prefix . '/' . $cache_dir;
                $basename = $cache_dir . '/' . $md5sum;
                $urlbase = $cache_url . '/' . $md5sum;
                $playfile = $basename . '.xml';
            }
            $playlist = $urlbase . '.xml';
            $list = array();
            for ($i = 0; $i < $sz; $i++) {
                if (!preg_match("/^((https?|ftp):)?\\/\\//", $url[$i])) {
                    $url = qualifiedUrl($url);
                }
                $ext = substr($media[$i], -3, 3);
                // XXX
                $list[] = '<title>' . $media[$i] . '</title>' . "\n" . '<location>' . $url[$i] . '</location>' . "\n";
            }
            $tracks = "<track>\n" . implode("</track>\n<track>\n", $list) . "</track>\n";
            // UTF-8 FIXME
            $xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
  <title>XSPF Playlist</title>
  <info>XSPF Playlist</info>
  <trackList>
{$tracks}
  </trackList>
</playlist>
XML;
            # check cache dir exists or not and make it
            if (!is_dir(dirname($playfile))) {
                $om = umask(00);
                _mkdir_p(dirname($playfile), 0777);
                umask($om);
            }
            if ($formatter->refresh or !file_exists($playfile)) {
                $fp = fopen($playfile, "w");
                fwrite($fp, $xml);
                fclose($fp);
            }
            $displayheight = $height;
            $height += $sz * 40;
            // XXX
            $addparam = "displayheight: '{$displayheight}'";
            $filelist = qualifiedUrl($playlist);
        } else {
            $filelist = $url[0];
        }
        $jw_script = <<<EOS
<p id="mediaplayer{$num}"></p>
<script type="text/javascript">
    (function() {
        var params = {
          allowfullscreen: "true"
        };

        var flashvars = {
          width: "{$width}",
          height: "{$height}",
          // image: "preview.jpg",
          {$addparam}
          file: "{$filelist}"
        };

        swfobject.embedSWF("{$_swf_prefix}/mediaplayer.swf","mediaplayer{$num}","{$width}","{$height}","0.0.9",
        "expressInstall.swf",flashvars,params);
    })();
</script>
EOS;
        return <<<EOS
      {$swfobject_script}{$jw_script}
EOS;
    } else {
        $out = '';
        $mysize = '';
        if (!empty($mywidth)) {
            $mysize .= 'width="' . $mywidth . 'px" ';
        }
        if (!empty($myheight)) {
            $mysize .= ' height="' . $myheight . 'px" ';
        }
        for ($i = 0, $sz = count($media); $i < $sz; $i++) {
            $mediainfo = 'External object';
            $classid = '';
            $objclass = '';
            $iframe = '';
            $custom = '';
            $object_prefered = false;
            // http://code.google.com/p/google-code-project-hosting-gadgets/source/browse/trunk/video/video.js
            if ($macro == 'youtube' && preg_match("@^([a-zA-Z0-9_-]+)(?:\\?.*)?\$@", $media[$i], $m) || preg_match("@(?:https?:)?//(?:[a-z-]+[.])?(?:youtube(?:[.][a-z-]+)+|youtu\\.be)/(?:watch[?].*v=|v/|embed/)?([a-z0-9_-]+)\$@i", $media[$i], $m)) {
                if ($object_prefered) {
                    $movie = "http://www.youtube.com/v/" . $m[1];
                    $type = 'type="application/x-shockwave-flash"';
                    $attr = $mysize . 'allowfullscreen="true" allowScriptAccess="always"';
                    $attr .= ' data="' . $movie . '?version=3' . '"';
                    $url[$i] = $movie;
                    $params = "<param name='movie' value='{$movie}?version=3'>\n" . "<param name='allowScriptAccess' value='always'>\n" . "<param name='allowFullScreen' value='true'>\n";
                } else {
                    $iframe = '//www.youtube.com/embed/' . $m[1];
                    $attr = 'frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen';
                    if (empty($mysize)) {
                        $attr .= ' width="500px" height="281px"';
                    } else {
                        $attr .= ' ' . $mysize;
                    }
                }
                $mediainfo = 'Youtube movie';
                $objclass = ' youtube';
            } else {
                if (preg_match('@^https?://(?:videofarm|tvpot)\\.daum\\.net/(?:.*?(?:clipid=|vid=|v/))?([a-zA-Z0-9%$]+)@i', $media[$i], $m)) {
                    // like as http://tvpot.daum.net/v/GCpMeZtuBnk%24
                    if (preg_match('@[0-9]+$@', $m[1])) {
                        // clipid case
                        $aurl = $media[$i];
                        $clipid = $m[1];
                        require_once "lib/HTTPClient.php";
                        // fetch tvpot.daum.net
                        $sc = new Cache_text('daumtvpot');
                        $maxage = 60 * 60;
                        if (empty($params['refresh']) and $sc->exists($aurl) and $sc->mtime($aurl) < time() + $maxage) {
                            $info = $sc->fetch($aurl);
                        } else {
                            // no cached info found.
                            if ($formatter->_macrocache and empty($params['call'])) {
                                return $formatter->macro_cache_repl('Play', $value);
                            }
                            if (empty($params['call'])) {
                                $formatter->_dynamic_macros['@Play'] = 1;
                            }
                            // try to fetch tvpot.daum.net
                            $http = new HTTPClient();
                            $save = ini_get('max_execution_time');
                            set_time_limit(0);
                            $http->timeout = 15;
                            // support proxy
                            if (!empty($DBInfo->proxy_host)) {
                                $http->proxy_host = $DBInfo->proxy_host;
                                if (!empty($DBInfo->proxy_port)) {
                                    $http->proxy_port = $DBInfo->proxy_port;
                                }
                            }
                            $http->sendRequest($aurl, array(), 'GET');
                            set_time_limit($save);
                            if ($http->status != 200) {
                                return '[[Media(' . $aurl . ')]]';
                            }
                            if (!empty($http->resp_body)) {
                                // search Open Graph url info
                                if (preg_match('@og:url"\\s+content="http://tvpot\\.daum\\.net/v/([^"]+)"@', $http->resp_body, $match)) {
                                    $info = array('vid' => $match[1], 'clipid' => $clipid);
                                    $sc->update($aurl, $info);
                                }
                            } else {
                                return '[[Media(' . $aurl . ')]]';
                            }
                        }
                        $m[1] = $info['vid'];
                    }
                    if ($object_prefered) {
                        $classid = "classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'";
                        $movie = "http://videofarm.daum.net/controller/player/VodPlayer.swf";
                        $type = 'type="application/x-shockwave-flash"';
                        $attr = 'allowfullscreen="true" allowScriptAccess="always" flashvars="vid=' . $m[2] . '&playLoc=undefined"';
                        if (empty($mysize)) {
                            $attr .= ' width="500px" height="281px"';
                        }
                        $url[$i] = $movie;
                        $params = "<param name='movie' value='{$movie}'>\n" . "<param name='flashvars' value='vid=" . $m[1] . "&playLoc=undefined'>\n" . "<param name='allowScriptAccess' value='always'>\n" . "<param name='allowFullScreen' value='true'>\n";
                    } else {
                        $iframe = '//videofarm.daum.net/controller/video/viewer/Video.html?play_loc=tvpot' . '&amp;jsCallback=false&amp;wmode=transparent&amp;vid=' . $m[1] . '&amp;autoplay=false&amp;permitWideScreen=true';
                        $attr = 'frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen';
                        if (empty($mysize)) {
                            $attr .= ' width="500px" height="281px"';
                        } else {
                            $attr .= ' ' . $mysize;
                        }
                    }
                    $mediainfo = 'Daum movie';
                    $objclass = ' daum';
                } else {
                    if ($macro == 'vimeo' && preg_match("@^(\\d+)\$@", $media[$i], $m) || preg_match("@(?:https?:)?//(?:player\\.)?vimeo\\.com\\/(?:video/)?(.*)\$@i", $media[$i], $m)) {
                        if ($object_prefered) {
                            $movie = "https://secure-a.vimeocdn.com/p/flash/moogaloop/5.2.55/moogaloop.swf?v=1.0.0";
                            $type = 'type="application/x-shockwave-flash"';
                            $attr = 'allowfullscreen="true" allowScriptAccess="always" flashvars="clip_id=' . $m[1] . '"';
                            if (empty($mysize)) {
                                $attr .= ' width="500px" height="281px"';
                            }
                            $url[$i] = $movie;
                            $params = "<param name='movie' value='{$movie}'>\n" . "<param name='flashvars' value='clip_id=" . $m[1] . "'>\n" . "<param name='allowScriptAccess' value='always'>\n" . "<param name='allowFullScreen' value='true'>\n";
                        } else {
                            $iframe = '//player.vimeo.com/video/' . $m[1] . '?portrait=0&color=333';
                            $attr = 'frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen';
                            if (empty($mysize)) {
                                $attr .= ' width="500px" height="281px"';
                            } else {
                                $attr .= ' ' . $mysize;
                            }
                        }
                        $mediainfo = 'Vimeo movie';
                        $objclass = ' vimeo';
                    } else {
                        if (($macro == 'niconico' || $macro == 'nicovideo') && preg_match("@((?:sm|nm)?\\d+)\$@i", $media[$i], $m) || preg_match("@(?:https?://(?:www|dic)\\.(?:nicovideo|nicozon)\\.(?:jp|net)/(?:v|watch)/)?((?:sm|nm)?\\d+)\$@i", $media[$i], $m)) {
                            $custom = '<script type="text/javascript" src="http://ext.nicovideo.jp/thumb_watch/' . $m[1];
                            $size = '';
                            $qprefix = '?';
                            if ($mywidth > 0) {
                                $size .= '?w=' . intval($mywidth);
                                $qprefix = '&amp;';
                            }
                            if ($myheight > 0) {
                                $size .= $qprefix . 'h=' . intval($myheight);
                            }
                            $custom .= $size;
                            $custom .= '"></script>';
                            $attr = 'frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen';
                            $mediainfo = 'Niconico';
                            $objclass = ' niconico';
                        } else {
                            if (preg_match("/(wmv|mpeg4|mp4|avi|asf)\$/", $media[$i], $m)) {
                                $classid = "classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95'";
                                $type = 'type="application/x-mplayer2"';
                                $attr = $mysize . 'autoplay="' . $play . '"';
                                $params = "<param name='FileName' value='" . $url[$i] . "' />\n" . "<param name='AutoStart' value='False' />\n" . "<param name='ShowControls' value='True' />";
                                $mediainfo = strtoupper($m[1]) . ' movie';
                            } else {
                                if (preg_match("/(wav|mp3|ogg)\$/", $media[$i], $m)) {
                                    $classid = "classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B'";
                                    $type = '';
                                    $attr = 'codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="30"';
                                    $attr .= ' autoplay="' . $play . '"';
                                    $params = "<param name='src' value='" . $url[$i] . "'>\n" . "<param name='AutoStart' value='{$play}' />";
                                    $mediainfo = strtoupper($m[1]) . ' sound';
                                } else {
                                    if (preg_match("/swf\$/", $media[$i])) {
                                        $type = 'type="application/x-shockwave-flash"';
                                        $classid = "classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'";
                                        $attr = 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"';
                                        $attr .= ' autoplay="' . $play . '"';
                                        $params = "<param name='movie' value='" . $url[$i] . "' />\n" . "<param name='AutoStart' value='{$play}' />";
                                    } else {
                                        if (preg_match("/\\.xap/", $media[$i])) {
                                            $type = 'type="application/x-silverlight-2"';
                                            $attr = $mysize . 'data="data:application/x-silverlight,"';
                                            $params = "<param name='source' value='" . $url[$i] . "' />\n";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $autoplay = 0;
            $play = 'false';
            if ($iframe) {
                $out .= <<<IFRAME
<div class='externalObject{$objclass}{$align}'><div>
<iframe class='external' src="{$iframe}" {$attr}></iframe>
<div><a alt='{$myurl}' onclick='javascript:openExternal(this, "inline-block"); return false;'><span>[{$mediainfo}]</span></a></div></div></div>
IFRAME;
            } else {
                if (isset($custom[0])) {
                    $out .= <<<OBJECT
<div class='externalObject{$objclass}'><div>
{$custom}
<div><a alt='{$myurl}' onclick='javascript:openExternal(this, "inline-block"); return false;'><span>[{$mediainfo}]</span></a></div></div></div>
OBJECT;
                } else {
                    $myurl = $url[$i];
                    $out .= <<<OBJECT
<div class='externalObject{$objclass}'><div>
<object class='external' {$classid} {$type} {$attr}>
{$params}
<param name="AutoRewind" value="True">
<embed {$type} src="{$myurl}" {$attr}></embed>
</object>
<div><a alt='{$myurl}' onclick='javascript:openExternal(this, "inline-block"); return false;'><span>[{$mediainfo}]</span></a></div></div></div>
OBJECT;
                }
            }
        }
    }
    if (empty($GLOBALS['js_macro_play'])) {
        $js = <<<JS
<script type='text/javascript'>
/*<![CDATA[*/
function openExternal(obj, display) {
  var el;
  (el = obj.parentNode.parentNode.firstElementChild) && (el.style.display = display);
}
/*]]>*/
</script>
JS;
        $formatter->register_javascripts($js);
        $GLOBALS['js_macro_play'] = 1;
    }
    return $out;
}
예제 #9
0
function macro_WikimediaCommons($formatter, $value, $params = array())
{
    global $DBInfo, $Config;
    // FIXME urldecode for some cases
    $value = urldecode($value);
    if (($p = strpos($value, ',')) !== false) {
        $arg = substr($value, $p + 1);
        $value = substr($value, 0, $p);
        $arg = preg_replace('@\\s*,\\s*@', ',', $arg);
        $arg = preg_replace('@\\s*=\\s*@', '=', $arg);
        $args = explode(',', $arg);
    }
    // check full url
    if (preg_match('@^https?://upload\\.wikimedia\\.org/wikipedia/commons/(thumb/)?./../([^/]+\\.(?:gif|jpe?g|png|svg))(?(1)/(\\d+px)-\\2)@', $value, $m)) {
        $remain = substr($value, strlen($m[0]));
        $value = $m[2];
        if (!empty($m[3])) {
            $width = intval($m[3]);
        }
    }
    $styles = array();
    foreach ($args as $arg) {
        $k = $v = '';
        if (($p = strpos($arg, '=')) !== false) {
            $k = substr($arg, 0, $p);
            $v = substr($arg, $p + 1);
        } else {
            continue;
        }
        $k = strtolower($k);
        switch ($k) {
            case 'width':
            case 'height':
                if (preg_match('@^(\\d+)(px|%)?$@', $v, $m)) {
                    if (isset($m[2]) && $m[2] == '%') {
                        $styles[$k] = $v;
                    } else {
                        $styles[$k] = $v;
                        ${$k} = intval($m[1]);
                    }
                }
                break;
            case 'align':
                $v = strtolower($v);
                if (in_array($v, array('left', 'right', 'center'))) {
                    $addClass = ' img' . ucfirst($v);
                }
                break;
        }
    }
    $common = new Cache_Text('wikicommons');
    $key = $value;
    if (isset($width)) {
        $key .= $width;
    }
    if (isset($height)) {
        $key .= '.h' . $height;
    }
    if (!empty($formatter->refresh) || ($images = $common->fetch($key)) === false) {
        $api_url = 'https://commons.wikimedia.org/w/api.php';
        $data = array('action' => 'query', 'titles' => 'Image:' . $value, 'prop' => 'imageinfo', 'iiprop' => 'extmetadata|url', 'format' => 'json', 'rawcontinue' => '1');
        if (!empty($width)) {
            $data['iiurlwidth'] = min(1280, $width);
        } else {
            if (!empty($height)) {
                $data['iiurlheight'] = min(1280, $height);
            } else {
                // default image width
                $data['iiurlwidth'] = 640;
            }
        }
        require_once dirname(__FILE__) . '/../lib/HTTPClient.php';
        $http = new HTTPClient();
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http->sendRequest($api_url, $data, 'POST');
        set_time_limit($save);
        // FIXME
        if ($http->status != 200) {
            return '';
        }
        $res = json_decode($http->resp_body);
        $images = $res->query->pages;
        $common->update($key, $images);
    }
    $image = current($images);
    $image_url = $image->imageinfo[0]->thumburl;
    $desc_url = $image->imageinfo[0]->descriptionurl;
    if (empty($styles['width'])) {
        $styles['width'] = $image->imageinfo[0]->thumbwidth . 'px';
    }
    $style = '';
    foreach ($styles as $k => $v) {
        $style .= $k . ':' . $v . ';';
    }
    if (!empty($style)) {
        $style = ' style="' . $style . '"';
    }
    $copyright = $image->imageinfo[0]->extmetadata->Copyrighted->value;
    $description = $image->imageinfo[0]->extmetadata->ImageDescription->value;
    $author = $image->imageinfo[0]->extmetadata->Artist->value;
    $license = $image->imageinfo[0]->extmetadata->License->value;
    if (!empty($formatter->fetch_images) && !empty($image_url)) {
        $image_url = $formatter->fetch_action . str_replace(array('&', '?'), array('%26', '%3f'), $image_url);
        // use thumbnails ?
        if (!empty($formatter->use_thumb_by_default)) {
            $image_url .= '&amp;thumbwidth=' . $formatter->thumb_width;
        }
    }
    $copyrighted = $copyright == 'True';
    $info = ($copyrighted ? '&copy;' : '(&#596;) ') . $author;
    if ($copyrighted) {
        $info .= " ({$license})";
    }
    $out = '<div class="externalImage">';
    if (empty($addClass)) {
        $cls = ' class="' . $addClass . '"';
    }
    $out .= "<div" . $cls . "><img src='{$image_url}'{$style}>";
    $out .= "<div class='info'>" . $info . ' from ' . "<a href='{$desc_url}' target='_blank'>WikiMedia Commons</a></div>";
    $out .= "</div>";
    if (!empty($DBInfo->wikimediacommons_use_description) && !empty($description)) {
        $out .= '<div class="desc">' . $description . '</div>';
    }
    $out .= "</div>\n";
    return $out;
}
예제 #10
0
파일: pull.php 프로젝트: reviforks/moniwiki
function macro_Pull($formatter, $pagename = '', $params = array())
{
    global $DBInfo;
    if (empty($pagename)) {
        $params['retval']['error'] = _("Empty PageName");
        return false;
    }
    if (empty($DBInfo->pull_url)) {
        $params['retval']['error'] = _("Empty \$pull_url");
        return false;
    }
    if (strpos($DBInfo->pull_url, '$PAGE') === false) {
        $url = $DBInfo->pull_url . _rawurlencode($pagename);
    } else {
        $url = preg_replace('/\\$PAGE/', _rawurlencode($pagename), $DBInfo->pull_url);
    }
    $url .= '?action=raw';
    require_once "lib/HTTPClient.php";
    $sz = 0;
    // set default params
    $maxage = !empty($DBInfo->pull_maxage) ? (int) $DBInfo->pull_maxage : 60 * 60 * 24 * 7;
    $timeout = !empty($DBInfo->pull_timeout) ? (int) $DBInfo->pull_timeout : 15;
    $maxage = (int) $maxage;
    // check connection
    $http = new HTTPClient();
    $sc = new Cache_text('mirrorinfo');
    $error = null;
    $headers = array();
    while ($sc->exists($pagename) and time() < $sc->mtime($pagename) + $maxage) {
        $info = $sc->fetch($pagename);
        if ($info == false) {
            break;
        }
        $sz = $info['size'];
        $etag = $info['etag'];
        $lastmod = $info['last-modified'];
        $error = !empty($info['error']) ? $info['error'] : null;
        // already retrived and found some error
        if (empty($params['refresh']) and !empty($error)) {
            return false;
        }
        // conditional get
        $headers['Cache-Control'] = 'maxage=0';
        $headers['If-Modified-Since'] = $lastmod;
        if (empty($DBInfo->pull_no_etag)) {
            $headers['If-None-Match'] = $etag;
        }
        // do not refresh for no error cases
        if (empty($error)) {
            unset($params['refresh']);
        }
        break;
    }
    // get file header
    $http->nobody = true;
    if (!empty($headers)) {
        $http->headers = array_merge($http->headers, $headers);
    }
    $http->sendRequest($url, array(), 'GET');
    if ($http->status == 304) {
        // not modified
        $params['retval']['status'] = 304;
        return true;
    }
    if ($http->status != 200) {
        $params['retval']['error'] = sprintf(_("Invalid Status %d"), $http->status);
        $params['retval']['status'] = $http->status;
        return false;
    } else {
        if (!empty($params['check'])) {
            $params['retval']['status'] = 200;
            return true;
        }
    }
    if (isset($http->resp_headers['content-length'])) {
        $sz = $http->resp_headers['content-length'];
    }
    $etag = '';
    $lastmod = '';
    if (isset($http->resp_headers['etag'])) {
        $etag = $http->resp_headers['etag'];
    }
    if (isset($http->resp_headers['last-modified'])) {
        $lastmod = $http->resp_headers['last-modified'];
    }
    $sc->update($pagename, array('size' => $sz, 'etag' => $etag, 'last-modified' => $lastmod));
    // size info
    if (is_numeric($sz)) {
        $unit = array('Bytes', 'KB', 'MB', 'GB');
        $tmp = $sz;
        for ($i = 0; $i < 4; $i++) {
            if ($tmp <= 1024) {
                break;
            }
            $tmp = $tmp / 1024;
        }
        $hsz = round($tmp, 2) . ' ' . $unit[$i];
    } else {
        $params['retval']['error'] = _("Can't get file size info");
        $params['retval']['mimetype'] = $mimetype;
        return false;
    }
    $pagefile = $DBInfo->getPageKey($pagename);
    $mtime = @strtotime($lastmod);
    $my_mtime = $formatter->page->mtime();
    // not exactly same file.
    if ($my_mtime != $mtime or abs($mtime - $my_mtime) > 60) {
        $params['refresh'] = 1;
    }
    // force refresh
    // real fetch job.
    if (!empty($params['refresh']) or !file_exists($pagefile)) {
        @unlink($pagefile);
        $fp = fopen($pagefile, 'w');
        if (!is_resource($fp)) {
            $params['retval']['error'] = sprintf(_("Fail to open %s"), $pagefile);
            return false;
        }
        // retry to get all info
        $http = new HTTPClient();
        $save = ini_get('max_execution_time');
        set_time_limit(0);
        $http->timeout = $timeout;
        $http->sendRequest($url, array(), 'GET');
        set_time_limit($save);
        if ($http->status != 200) {
            fclose($fp);
            unlink($pagefile);
            // Error found! save error status to the info cache
            $params['retval']['error'] = !empty($http->error) ? $http->error : sprintf(_("Invalid Status %d"), $http->status);
            $params['retval']['status'] = $http->status;
            $params['retval']['etag'] = $etag;
            $params['retval']['last-modified'] = $lastmod;
            $params['retval']['size'] = $sz;
            $sc->update($pagename, array('size' => $sz, 'etag' => $mimetype, 'last-modified' => $lastmod, 'error' => $http->error, 'status' => $params['retval']['status']));
            return false;
        }
        if (!empty($http->resp_body)) {
            fwrite($fp, $http->resp_body);
        }
        fclose($fp);
        //$mtime = @strtotime($lastmod);
        //touch($pagefile, $mtime);
        // remove PI cache to update
        $pi = new Cache_text('PI');
        $pi->remove($formatter->page->name);
        // update error status.
        if (!empty($error)) {
            $sc->update($pagename, array('size' => $sz, 'etag' => $etag, 'last-modified' => $lastmod));
        }
        $loc = $formatter->link_url($pagename);
        $loc = preg_replace('/&amp;/', '&', $loc);
        $formatter->send_header(array('Status: 302', 'Location: ' . $loc), $params);
        echo 'Successfully fetched';
    } else {
        echo 'Not modified';
    }
    return null;
}