/**
  * Returns original filename if exists
  */
 function _getOriginalFileName($id)
 {
     $metaFilePath = mediaMetaFN($id, '.filename');
     $meta = unserialize(io_readFile($metaFilePath, false));
     if (empty($meta['filename'])) {
         // check old meta file (for backward compatibility)
         $filename = parent::_getOriginalFileName($id);
         // move old meta file to media_meta directory
         if ($filename !== false) {
             $oldMetaFilePath = metaFN($id, '.filename');
             io_rename($oldMetaFilePath, $metaFilePath);
         }
         return $filename;
     } else {
         return $this->common->_sanitizeFileName($meta['filename']);
     }
 }
Exemplo n.º 2
0
/**
 * Return a list of page revisions numbers
 * Does not guarantee that the revision exists in the attic,
 * only that a line with the date exists in the changelog.
 * By default the current revision is skipped.
 *
 * id:    the page of interest
 * first: skip the first n changelog lines
 * num:   number of revisions to return
 *
 * The current revision is automatically skipped when the page exists.
 * See $INFO['meta']['last_change'] for the current revision.
 *
 * For efficiency, the log lines are parsed and cached for later
 * calls to getRevisionInfo. Large changelog files are read
 * backwards in chunks until the requested number of changelog
 * lines are recieved.
 *
 * @author Ben Coburn <*****@*****.**>
 * @author Kate Arzamastseva <*****@*****.**>
 */
function getRevisions($id, $first, $num, $chunk_size = 8192, $media = false)
{
    global $cache_revinfo;
    $cache =& $cache_revinfo;
    if (!isset($cache[$id])) {
        $cache[$id] = array();
    }
    $revs = array();
    $lines = array();
    $count = 0;
    if ($media) {
        $file = mediaMetaFN($id, '.changes');
    } else {
        $file = metaFN($id, '.changes');
    }
    $num = max($num, 0);
    $chunk_size = max($chunk_size, 0);
    if ($first < 0) {
        $first = 0;
    } else {
        if (!$media && @file_exists(wikiFN($id)) || $media && @file_exists(mediaFN($id))) {
            // skip current revision if the page exists
            $first = max($first + 1, 0);
        }
    }
    if (!@file_exists($file)) {
        return $revs;
    }
    if (filesize($file) < $chunk_size || $chunk_size == 0) {
        // read whole file
        $lines = file($file);
        if ($lines === false) {
            return $revs;
        }
    } else {
        // read chunks backwards
        $fp = fopen($file, 'rb');
        // "file pointer"
        if ($fp === false) {
            return $revs;
        }
        fseek($fp, 0, SEEK_END);
        $tail = ftell($fp);
        // chunk backwards
        $finger = max($tail - $chunk_size, 0);
        while ($count < $num + $first) {
            fseek($fp, $finger);
            $nl = $finger;
            if ($finger > 0) {
                fgets($fp);
                // slip the finger forward to a new line
                $nl = ftell($fp);
            }
            // was the chunk big enough? if not, take another bite
            if ($nl > 0 && $tail <= $nl) {
                $finger = max($finger - $chunk_size, 0);
                continue;
            } else {
                $finger = $nl;
            }
            // read chunk
            $chunk = '';
            $read_size = max($tail - $finger, 0);
            // found chunk size
            $got = 0;
            while ($got < $read_size && !feof($fp)) {
                $tmp = @fread($fp, max($read_size - $got, 0));
                if ($tmp === false) {
                    break;
                }
                //error state
                $got += strlen($tmp);
                $chunk .= $tmp;
            }
            $tmp = explode("\n", $chunk);
            array_pop($tmp);
            // remove trailing newline
            // combine with previous chunk
            $count += count($tmp);
            $lines = array_merge($tmp, $lines);
            // next chunk
            if ($finger == 0) {
                break;
            } else {
                $tail = $finger;
                $finger = max($tail - $chunk_size, 0);
            }
        }
        fclose($fp);
    }
    // skip parsing extra lines
    $num = max(min(count($lines) - $first, $num), 0);
    if ($first > 0 && $num > 0) {
        $lines = array_slice($lines, max(count($lines) - $first - $num, 0), $num);
    } else {
        if ($first > 0 && $num == 0) {
            $lines = array_slice($lines, 0, max(count($lines) - $first, 0));
        } else {
            if ($first == 0 && $num > 0) {
                $lines = array_slice($lines, max(count($lines) - $num, 0));
            }
        }
    }
    // handle lines in reverse order
    for ($i = count($lines) - 1; $i >= 0; $i--) {
        $tmp = parseChangelogLine($lines[$i]);
        if ($tmp !== false) {
            $cache[$id][$tmp['date']] = $tmp;
            $revs[] = $tmp['date'];
        }
    }
    return $revs;
}
Exemplo n.º 3
0
/**
 * Restores an old revision of a media file
 *
 * @param string $image
 * @param int $rev
 * @param int $auth
 * @return string - file's id
 * @author Kate Arzamastseva <*****@*****.**>
 */
function media_restore($image, $rev, $auth)
{
    global $conf;
    if ($auth < AUTH_UPLOAD || !$conf['mediarevisions']) {
        return false;
    }
    $removed = !file_exists(mediaFN($image)) && file_exists(mediaMetaFN($image, '.changes'));
    if (!$image || !file_exists(mediaFN($image)) && !$removed) {
        return false;
    }
    if (!$rev || !file_exists(mediaFN($image, $rev))) {
        return false;
    }
    list(, $imime, ) = mimetype($image);
    $res = media_upload_finish(mediaFN($image, $rev), mediaFN($image), $image, $imime, true, 'copy');
    if (is_array($res)) {
        msg($res[0], $res[1]);
        return false;
    }
    return $res;
}
Exemplo n.º 4
0
/**
 * Prints the third column in full-screen media manager
 * Depending on the opened tab this may be details of the
 * selected file, the meta editing dialog or
 * list of file revisions
 *
 * @author Kate Arzamastseva <*****@*****.**>
 */
function tpl_mediaFileDetails($image, $rev)
{
    global $AUTH, $NS, $conf, $DEL, $lang, $INPUT;
    $removed = !file_exists(mediaFN($image)) && file_exists(mediaMetaFN($image, '.changes')) && $conf['mediarevisions'];
    if (!$image || !file_exists(mediaFN($image)) && !$removed || $DEL) {
        return;
    }
    if ($rev && !file_exists(mediaFN($image, $rev))) {
        $rev = false;
    }
    if (isset($NS) && getNS($image) != $NS) {
        return;
    }
    $do = $INPUT->str('mediado');
    $opened_tab = $INPUT->str('tab_details');
    $tab_array = array('view');
    list(, $mime) = mimetype($image);
    if ($mime == 'image/jpeg') {
        $tab_array[] = 'edit';
    }
    if ($conf['mediarevisions']) {
        $tab_array[] = 'history';
    }
    if (!$opened_tab || !in_array($opened_tab, $tab_array)) {
        $opened_tab = 'view';
    }
    if ($INPUT->bool('edit')) {
        $opened_tab = 'edit';
    }
    if ($do == 'restore') {
        $opened_tab = 'view';
    }
    media_tabs_details($image, $opened_tab);
    echo '<div class="panelHeader"><h3>';
    list($ext) = mimetype($image, false);
    $class = preg_replace('/[^_\\-a-z0-9]+/i', '_', $ext);
    $class = 'select mediafile mf_' . $class;
    $tabTitle = '<strong><a href="' . ml($image) . '" class="' . $class . '" title="' . $lang['mediaview'] . '">' . $image . '</a>' . '</strong>';
    if ($opened_tab === 'view' && $rev) {
        printf($lang['media_viewold'], $tabTitle, dformat($rev));
    } else {
        printf($lang['media_' . $opened_tab], $tabTitle);
    }
    echo '</h3></div>' . NL;
    echo '<div class="panelContent">' . NL;
    if ($opened_tab == 'view') {
        media_tab_view($image, $NS, $AUTH, $rev);
    } elseif ($opened_tab == 'edit' && !$removed) {
        media_tab_edit($image, $NS, $AUTH);
    } elseif ($opened_tab == 'history' && $conf['mediarevisions']) {
        media_tab_history($image, $NS, $AUTH);
    }
    echo '</div>' . NL;
}
Exemplo n.º 5
0
 /**
  * Returns path to changelog
  *
  * @return string path to file
  */
 protected function getChangelogFilename()
 {
     return mediaMetaFN($this->id, '.changes');
 }