Пример #1
0
function json_clean($str)
{
    $r = ensureUTF8($str);
    $r = str_replace(array('\\"', '\\\''), array('"', "'"), $r);
    $r = htmlentities($r, ENT_QUOTES, 'UTF-8');
    return $r;
}
Пример #2
0
 public function __construct(array $config = array())
 {
     // needed for ensure thumbs
     // this is on the assumption, that not very much time was wasted
     // in the header section on other stuff
     $this->starttime = microtime(true);
     // merge given (precedence) and default config
     $this->config = (object) array_merge(self::$defaultConfig, $config);
     $c =& $this->config;
     // shorthand
     $this->ROOT = rtrim($_SERVER['DOCUMENT_ROOT'], '\\/');
     // normalize trailing
     // URL sanitation -------------------------------------------------
     if ($c->url === null) {
         // TODO split up, chooping of should happen always,
         //   requiest_uri only be taken if...
         $c->url = strtok($_SERVER['REQUEST_URI'], '?');
         // chop off any query params
     }
     $c->url = rtrim($c->url, '/');
     // normalize slash
     $c->url = rawurldecode($c->url);
     $c->origUrl = rtrim($c->url, '/');
     // keep a copy for thumb
     $c->url = utf8_decode($c->url);
     // !! here, utf8 gets lost !!
     // normalize url to forward slashes
     $c->url = str_replace('\\', '/', $c->url);
     // needed for title and breadcrumb
     if (empty($c->url)) {
         // root level?
         $this->path = array();
     } else {
         $this->path = explode('/', trim($c->url, '/'));
     }
     $entries = scandir($this->ROOT . $c->url);
     // http://stackoverflow.com/questions/8692764/readdir-vs-scandir
     // COULDDO  "dir /x" hack, around here
     //
     // rough draft:
     // dir /x
     // 11.04.2012  12:20            63.650 WORDPR~3.ZIP wordpress-05_threeKiwis-not-working.zip
     // Match this ( while avoiding matching header/footer crud )
     // ([\d\.]{10})\s+([\d\.]{5})
     // - perhaps beware of different locale time displays?  (12:20pm?)
     enforce(null != $entries);
     // parse directory -------------------------------------------------------------------------
     foreach ($entries as $entry) {
         $entryPath = $this->ROOT . $c->url . '/' . $entry;
         //show readme-content (but don't list file itself)
         if (preg_match('/' . $c->README . '/i', $entry) == true) {
             $lines = @file($entryPath);
             $this->readme_content .= "\n" . self::$tabs[$c->indent] . "<p>\n";
             foreach ($lines as $line) {
                 $this->readme_content .= self::$tabs[$c->indent] . trim($line) . "<br/>\n";
             }
             $this->readme_content .= self::$tabs[$c->indent] . "</p>\n";
         }
         // filtering stage --------------------------------------------------------------
         // make sure, excludes and moot directories don't come through...
         // avoid security breaches by ../sub/../sub ...
         if ($entry == '.' || $entry === '..') {
             continue;
         }
         // never show (linux-)hidden files or folders
         if (self::matches($entry, array('/^\\./i'))) {
             continue;
         }
         // Blacklist applies to files and folders
         if (self::matches($entry, $this->config->BLACKLIST)) {
             continue;
         }
         // additional stuff not desired to be shown
         if (self::matches($entry, $this->config->BLACKLIST_CUSTOM)) {
             continue;
         }
         // Whitelist applies to files (since folders may contain dots, not extension matching)
         if (!is_dir($entryPath) && !self::matches($entry, $this->config->WHITELIST)) {
             continue;
         }
         $parts = pathinfo($entry);
         $base = $parts['basename'];
         // full filenname, saves (no) dot hassles..
         $ext = isset($parts['extension']) ? $parts['extension'] : '';
         // (defaults to default during rendering)
         $ext = strtolower($ext);
         // normalize to lowercase
         // COULDDO: dir /x windows hack to support full utf-8 files
         $item = array('type' => 'file', 'title' => 'Ansehen', 'filename' => ensureUTF8($base), 'link' => rawurlencode(ensureUTF8($base)), 'ext' => rawurlencode(strtolower($ext)), 'icon' => null, 'size' => null);
         if (is_dir($entryPath)) {
             // handle Directories
             $item['title'] = 'Verzeichnis öffnen';
             $item['type'] = 'dir';
             $this->dirCount++;
             $item['link'] .= '/';
             //tack a slash onto dir-paths
         } else {
             // Handle .url Windows Favorite/Bookmark) Files
             if ($item['ext'] == 'url') {
                 $urlfile = file($this->ROOT . $c->url . '/' . $item['filename']);
                 foreach ($urlfile as $line_num => $line) {
                     if (strstr(trim($line), 'URL=')) {
                         $item['type'] = 'link';
                         $item['link'] = trim(substr($line, 4));
                         $item['filesize'] = "(link)";
                     }
                 }
             } else {
                 if (is_file($entryPath)) {
                     $item['size'] = filesize($entryPath);
                     // Window issue: negative size could mean very large (GB files)
                     if ($item['size'] < 0 && DIRECTORY_SEPARATOR === '\\') {
                         $filesystem = new COM('Scripting.FileSystemObject');
                         $fo = $filesystem->GetFile($entryPath);
                         $item['size'] = $fo->Size();
                         trace('windows adjustment to ' . $item['size']);
                     }
                 }
             }
             // statistics
             $this->fileCount++;
             // count (thumb-able) images for auto-mode
             if ($item['type'] === 'file' && isset(self::$thumb_support[$item['ext']])) {
                 $this->thumbCount++;
                 $item['thumbID'] = "img" . $this->thumbCount;
                 /* needed for slideshow/direct linking */
             }
             if ($item['size'] != null) {
                 // n/a for links, undectable for some utf-8 files
                 $this->totalSize += $item['size'];
             }
         }
         // determine icon to use:
         // apply icon mappings ( e.g. png, tif => jpg)
         // (actual $ext does survive, needed for thumnail decision)
         $mappedExt = $ext;
         if (isset(self::$mappings[$ext])) {
             $mappedExt = self::$mappings[$ext];
         }
         switch ($item['type']) {
             case 'file':
             case 'link':
                 // .lnk plays along
                 $iconUrl = $mappedExt . '.' . $c->ICON_EXT;
                 break;
             case 'dir':
                 $iconUrl = $c->ICON_FOLDER;
                 break;
             default:
                 fail('unknown type');
         }
         $item['icon'] = $iconUrl;
         if ($item['type'] == 'dir') {
             //add directories in front
             array_unshift($this->fileList, $item);
         } else {
             $this->fileList[] = $item;
         }
     }
     //foreach $entry
     // lastly, prepend parent folder. avoid on website top-level.
     if ($c->SHOWPARENT && !empty($this->path)) {
         array_unshift($this->fileList, array('type' => 'dir', 'title' => 'eine Ebene höher', 'filename' => '.. (one level up)', 'icon' => $c->ICON_PARENT, 'link' => '..', 'ext' => '', 'size' => null));
     }
     // var_dump($this->fileList);
 }
Пример #3
-1
 public static function getAsJson($file, $force = false)
 {
     $json = array();
     getimagesize($file, $info);
     if (isset($info["APP13"])) {
         $iptc = iptcparse($info["APP13"]);
         if (isset($iptc['2#120'])) {
             $caption = implode('|', $iptc['2#120']);
             // nb: '|' should never actually appear
             $caption = ensureUTF8($caption);
             // since could be 'local' encoding
             $caption = mysql_escape_string($caption);
             // safety. stackoverflow.com/q/1162491
             $json['caption'] = $caption;
         }
         if (isset($iptc['2#025'])) {
             $json['keywords'] = $iptc['2#025'];
         }
         // keep as array
         set_error_handler("ignoreAnyError", E_ALL);
         // TOTEST, currently no exif enabled on localhost
         if (function_exists('exif_read_data')) {
             $json['exif'] = exif_read_data($tmpfile, 0, false);
         }
         // fails on a very few files (corrupt EXIF)
         restore_error_handler();
     }
     Log::info("json for :" . $file);
     Log::info($json);
     return $json;
 }