/**
  * Calculates infos on the given file and returns an array containing these infos
  */
 function GetFileInfo($url)
 {
     global $serendipity;
     $this->log("GetFileInfo for {$url}");
     $fileInfo = array();
     //caching metadata
     $cacheOptions = array('lifeTime' => '2592000', 'automaticSerialization' => true, 'cacheDir' => $serendipity['serendipityPath'] . 'templates_c/');
     if (serendipity_db_bool($this->get_config('use_cache', 'true'))) {
         $this->log("GetFileInfo: Trying cached infos");
         //md5 for not having strange characters in that id..
         $cacheId = md5($url) . '.2';
         include_once S9Y_PEAR_PATH . "Cache/Lite.php";
         $cache = new Cache_Lite($cacheOptions);
         if ($fileInfo = $cache->get($cacheId)) {
             $this->log("GetFileInfo: Cached infos found in file {$cacheId}");
             //return directly on cache hit
             return $fileInfo;
         }
     }
     //cache miss! -> get data, store it in cache and return.
     // translate pontential relative url to absolute url
     if (preg_match('@https?://@', $url)) {
         $absolute_url = $url;
     } else {
         $absolute_url = $this->GetHostUrl() . $url;
     }
     if ($this->debug) {
         $fileInfo['absolute_url'] = $absolute_url;
     }
     // Now remove configured base URL
     $rel_path = str_replace($serendipity['baseURL'], "", $absolute_url);
     if ($this->debug) {
         $fileInfo['rel_path'] = $rel_path;
     }
     // do we have a local file here?
     //$localMediaFile = $serendipity['serendipityPath'] . $urlParts['path'];
     $localMediaFile = $serendipity['serendipityPath'] . $rel_path;
     $fileInfo['localMediaFile'] = $localMediaFile;
     $this->log("Absolute_url: {$absolute_url} - Relative: {$localMediaFile}");
     // Remember extension of file
     list($sName, $fileInfo['extension']) = serendipity_parseFileName($localMediaFile);
     if (file_exists($localMediaFile)) {
         $this->log("GetFileInfo: Local file exists");
         $fileInfo['length'] = filesize($localMediaFile);
         $fileInfo['md5'] = md5_file($localMediaFile);
         $this->GetID3Infos($localMediaFile, $fileInfo);
         $this->log(print_r($fileInfo, true));
         // Set default
         $fileInfo['mime'] = $this->getFileMime($fileInfo['extension'], $fileInfo['mime']);
     } elseif (preg_match('@https?://@', $url)) {
         include_once S9Y_PEAR_PATH . 'HTTP/Request.php';
         if (function_exists('serendipity_request_start')) {
             serendipity_request_start();
         }
         $this->Log("Execute HTTP_Request for {$url}");
         $http = new HTTP_Request($url);
         $http->setMethod(HTTP_REQUEST_METHOD_HEAD);
         if (!PEAR::isError($http->sendRequest(false))) {
             $fileInfo['length'] = intval($http->getResponseHeader('content-length'));
             $fileInfo['md5'] = $http->getResponseHeader('content-md5');
             //will return false if not present
             $fileInfo['mime'] = $http->getResponseHeader('content-type');
             $this->Log("Filling MIME with HTTP Header: " . print_r($fileInfo, true));
         }
         if (function_exists('serendipity_request_end')) {
             serendipity_request_end();
         }
     } else {
         // Not found locally and no URL
         $fileInfo['notfound'] = true;
     }
     if (serendipity_db_bool($this->get_config('use_cache', 'true'))) {
         $cache->save($fileInfo, $cacheId);
     }
     return $fileInfo;
 }
/**
 * Parses an existing filename and increases the filecount.
 *
 * @param  string   The (duplicate) filename
 * @param  string   The full path to the (duplicate) filename
 * @param  string   The directory of the (duplicate) filename
 * @param  boolean  Show new filename?
 * @return string   The new filename
 *
 */
function serendipity_imageAppend(&$tfile, &$target, $dir, $echo = true)
{
    static $safe_bail = 20;
    $realname = $tfile;
    list($filebase, $extension) = serendipity_parseFileName($tfile);
    $cnum = 1;
    if (preg_match('@^(.*)([0-9]+)$@', $filebase, $match)) {
        $cnum = $match[2];
        $filebase = $match[1];
    }
    $i = 0;
    while ($i <= $safe_bail && file_exists($dir . $filebase . $cnum . '.' . $extension)) {
        $cnum++;
    }
    // Check if the file STILL exists and append a MD5 if that's the case. That should be unique enough.
    if (file_exists($dir . $filebase . $cnum . '.' . $extension)) {
        $cnum = md5(time() . $filebase);
    }
    // Those variables are passed by reference!
    $tfile = $filebase . $cnum . '.' . $extension;
    $target = $dir . $tfile;
    if ($echo) {
        printf(FILENAME_REASSIGNED . '<br />', htmlspecialchars($tfile));
    }
    return $realname;
}
/**
 * Parses an existing filename and increases the filecount.
 *
 * @param  string   The (duplicate) filename
 * @param  string   The full path to the (duplicate) filename
 * @param  string   The directory of the (duplicate) filename
 * @param  boolean  Show new filename?
 * @return string   The new filename
 *
 */
function serendipity_imageAppend(&$tfile, &$target, $dir, $echo = true)
{
    static $safe_bail = 20;
    $realname = $tfile;
    list($filebase, $extension) = serendipity_parseFileName($tfile);
    $cnum = 1;
    if (preg_match('@^(.*)([0-9]+)$@', $filebase, $match)) {
        $cnum = $match[2];
        $filebase = $match[1];
    }
    $i = 0;
    while ($i <= $safe_bail && file_exists($dir . $filebase . $cnum . (empty($extension) ? '' : '.' . $extension))) {
        $cnum++;
    }
    // Check if the file STILL exists and append a MD5 if that's the case. That should be unique enough.
    if (file_exists($dir . $filebase . $cnum . (empty($extension) ? '' : '.' . $extension))) {
        $cnum = md5(time() . $filebase);
    }
    // Those variables are passed by reference!
    $tfile = $filebase . $cnum . (empty($extension) ? '' : '.' . $extension);
    $target = $dir . $tfile;
    if ($echo) {
        echo '<span class="msg_success"><span class="icon-ok-circled" aria-hidden="true"></span> <strong>';
        printf(FILENAME_REASSIGNED . '<br />', serendipity_specialchars($tfile));
        echo "</strong></span>\n";
    }
    return $realname;
}
 /**
  * Parses a quickblog entry to replace the pattern with the quickblog object
  * Make sure to not produce any output or error message here, since eventDatas
  * next out file is the streamed serendipitry_editor.js file
  *
  * @param   string  $path       A filepath match
  * @param   string  $body       Referenced entry body
  * @return  string  $content
  */
 function parse_quickblog_post($path, &$body)
 {
     global $serendipity;
     preg_match('@<!--quickblog:(.+\\|)+(.+)-->@imsU', $body, $target);
     $path = str_replace($target[1], '', $path);
     $body = str_replace($target[1], '', $body);
     //check for non-image object
     if (!isset($target) && empty($target)) {
         return $body;
     }
     $file = basename($path);
     $dir = dirname($path) . '/';
     $t = serendipity_parseFileName($file);
     $f = $t[0];
     $suf = $t[1];
     $infile = $dir . $file;
     $s9yimgID = (int) $this->getImageIdByUrl($infile);
     $outfile = $dir . $f . '.quickblog.' . $suf;
     // check for existing image.quickblog thumb (see change in backend_image_addHotlink) else change to default thumbnail name
     if (!file_exists($outfile)) {
         $outfile = $dir . $f . '.' . $serendipity['thumbSuffix'] . '.' . $suf;
     }
     if (function_exists('exif_read_data') && file_exists($infile) && !serendipity_db_bool($this->get_config('force_jhead'))) {
         $exif = @exif_read_data($infile);
         $exif_mode = 'internal';
     } elseif (file_exists($infile)) {
         $exif_mode = 'jhead';
         $exif_raw = explode("\n", @`jhead {$infile}`);
         $exif = array();
         foreach ((array) $exif_raw as $line) {
             preg_match('@^(.+):(.+)$@U', $line, $data);
             $key = preg_replace('@[^a-z0-9]@i', '_', trim($data[1]));
             if (empty($key)) {
                 continue;
             }
             $exif[$key] .= trim($data[2]) . "\n";
         }
         if (count($exif) < 1) {
             $exif = false;
         }
     } else {
         $exif = false;
         $exif_mode = 'none';
     }
     $http_infile = $this->httpize($infile);
     $http_outfile = $this->httpize($outfile);
     // create link targets
     $totarget = str_replace('|', '', $target[1]);
     $linktarget = '';
     switch ($totarget) {
         case '_blank':
             $linktarget = ' target="_blank"';
             break;
         case 'js':
             try {
                 list($width, $height, $type, $attr) = getimagesize("{$infile}");
             } catch (Exception $e) {
                 echo ERROR_SOMETHING . ': ' . $e->getMessage();
             }
             $linktarget = ' onclick="F1 = window.open(\'' . $http_infile . '\',\'Zoom\',\'height=' . $height . ',width=' . $width . ',top=\'+ (screen.height-' . $height . ')/2 +\',left=\'+ (screen.width-' . $width . ')/2 +\',toolbar=no,menubar=no,location=no,resize=1,resizable=1,scrollbars=yes\'); return false;"';
             break;
         case 'plugin':
             $linktarget = ' id="s9yisphref' . $s9yimgID . '" onclick="javascript:this.href = this.href + \'&amp;serendipity[from]=\' + self.location.href;"';
             $linkto = $serendipity['serendipityHTTPPath'] . 'serendipity_admin_image_selector.php?serendipity[step]=showItem&amp;serendipity[image]=' . $s9yimgID;
             $http_infile = $this->httpize($linkto);
             break;
     }
     $quickblog = array('html5' => $serendipity['wysiwyg'] || $serendipity['version'][0] > 1 ? true : false, 'image' => $http_outfile, 'fullimage' => $http_infile, 'body' => preg_replace('@(<!--quickblog:.+-->)@imsU', '', $body), 'imageid' => $s9yimgID, 'target' => $linktarget, 'istarget' => $totarget, 'exif' => &$exif, 'exif_mode' => $exif_mode);
     if (!is_object($serendipity['smarty'])) {
         serendipity_smarty_init();
     }
     $serendipity['smarty']->assign('quickblog', $quickblog);
     $content = $this->parseTemplate('quickblog.tpl');
     return $content;
 }
 /**
  * Retrieves all the images from the img/ directory
  *
  * @return array an array with 'fname', 'width', and 'height' entries for
  *     each valid image file in the img/ directory
  */
 function getImageFiles()
 {
     $path = dirname(__FILE__) . "/img";
     $images = array();
     $folder = opendir($path);
     while (false !== ($filename = readdir($folder))) {
         if ($filename != "ajax-loader.gif") {
             $parts = serendipity_parseFileName($filename);
             $img_data = serendipity_getimagesize($path . '/' . $filename);
             if (!isset($img_data['noimage'])) {
                 // Curly braces are just a different syntax of associative array assignment
                 $images[$filename] = array('fname' => $filename, 'width' => $img_data[0], 'height' => $img_data[1]);
             }
         }
     }
     closedir($folder);
     ksort($images);
     return $images;
 }