Merge pull request #491 from shish/more_https

More https
This commit is contained in:
Shish 2015-07-20 23:28:21 +01:00
commit f3c9f57e62
6 changed files with 146 additions and 177 deletions

View File

@ -279,7 +279,7 @@ class Image {
*/
public static function count_pages($tags=array()) {
assert(is_array($tags));
global $config, $database;
global $config;
return ceil(Image::count_images($tags) / $config->get_int('index_images'));
}
@ -385,33 +385,7 @@ class Image {
* @return string
*/
public function get_image_link() {
global $config;
$image_ilink = $config->get_string('image_ilink'); // store a copy for speed.
if( !empty($image_ilink) ) { /* empty is faster than strlen */
if(!startsWith($image_ilink, "http://") && !startsWith($image_ilink, "/")) {
$image_ilink = make_link($image_ilink);
}
return $this->parse_link_template($image_ilink);
}
else if($config->get_bool('nice_urls', false)) {
return $this->parse_link_template(make_link('_images/$hash/$id%20-%20$tags.$ext'));
}
else {
return $this->parse_link_template(make_link('image/$id.$ext'));
}
}
/**
* Get a short link to the full size image
*
* @deprecated
* @return string
*/
public function get_short_link() {
global $config;
return $this->parse_link_template($config->get_string('image_slink'));
return $this->get_link('image_ilink', '_images/$hash/$id%20-%20$tags.$ext', 'image/$id.jpg');
}
/**
@ -420,21 +394,33 @@ class Image {
* @return string
*/
public function get_thumb_link() {
return $this->get_link('image_tlink', '_thumbs/$hash/thumb.jpg', 'thumb/$id.jpg');
}
/**
* Check configured template for a link, then try nice URL, then plain URL
*
* @param $template
* @param $nice
* @param $plain
* @return string
*/
private function get_link($template, $nice, $plain) {
global $config;
$image_tlink = $config->get_string('image_tlink'); // store a copy for speed.
$image_link = $config->get_string($template);
if( !empty($image_tlink) ) { /* empty is faster than strlen */
if(!startsWith($image_tlink, "http://") && !startsWith($image_tlink, "/")) {
$image_tlink = make_link($image_tlink);
if(!empty($image_link)) {
if(!(strpos($link, "://") > 0) && !startsWith($image_link, "/")) {
$image_link = make_link($image_link);
}
return $this->parse_link_template($image_tlink);
return $this->parse_link_template($image_link);
}
else if($config->get_bool('nice_urls', false)) {
return $this->parse_link_template(make_link('_thumbs/$hash/thumb.jpg'));
return $this->parse_link_template(make_link($nice));
}
else {
return $this->parse_link_template(make_link('thumb/$id.jpg'));
return $this->parse_link_template(make_link($plain));
}
}
@ -1286,6 +1272,58 @@ function move_upload_to_archive(DataUploadEvent $event) {
return true;
}
/**
* Add a directory full of images
*
* @param $base string
* @return string
*/
function add_dir(/*string*/ $base) {
$list = "";
foreach(list_files($base) as $full_path) {
$short_path = str_replace($base, "", $full_path);
$filename = basename($full_path);
$tags = path_to_tags($short_path);
$list .= "<br>".html_escape("$short_path (".str_replace(" ", ", ", $tags).")... ");
try {
add_image($full_path, $filename, $tags);
$list .= "ok\n";
}
catch(UploadException $ex) {
$list .= "failed: ".$ex->getMessage()."\n";
}
}
return $list;
}
/**
* @param $tmpname
* @param $filename
* @param $tags
* @throws UploadException
*/
function add_image(/*string*/ $tmpname, /*string*/ $filename, /*string*/ $tags) {
assert(file_exists($tmpname));
$pathinfo = pathinfo($filename);
if(!array_key_exists('extension', $pathinfo)) {
throw new UploadException("File has no extension");
}
$metadata = array();
$metadata['filename'] = $pathinfo['basename'];
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = $tags;
$metadata['source'] = null;
$event = new DataUploadEvent($tmpname, $metadata);
send_event($event);
if($event->image_id == -1) {
throw new UploadException("File type not recognised");
}
}
/**
* Given a full size pair of dimensions, return a pair scaled down to fit
* into the configured thumbnail square, with ratio intact

View File

@ -402,7 +402,7 @@ function modify_url($url, $changes) {
* @return string
*/
function make_http(/*string*/ $link) {
if(strpos($link, "ttp://") > 0) {
if(strpos($link, "://") > 0) {
return $link;
}
@ -787,7 +787,6 @@ function get_session_ip(Config $config) {
* conflicts from multiple installs within one domain.
*/
function get_prefixed_cookie(/*string*/ $name) {
global $config;
$full_name = COOKIE_PREFIX."_".$name;
if(isset($_COOKIE[$full_name])) {
return $_COOKIE[$full_name];
@ -808,7 +807,6 @@ function get_prefixed_cookie(/*string*/ $name) {
* @param string $path
*/
function set_prefixed_cookie($name, $value, $time, $path) {
global $config;
$full_name = COOKIE_PREFIX."_".$name;
setcookie($full_name, $value, $time, $path);
}
@ -1287,6 +1285,64 @@ function full_copy($source, $target) {
}
}
/**
* Return a list of all the regular files in a directory and subdirectories
*
* @param string $base
* @param string $_sub_dir
* @return array file list
*/
function list_files(/*string*/ $base, $_sub_dir="") {
assert(is_dir($base));
$file_list = array();
$files = array();
$dir = opendir("$base/$_sub_dir");
while($f = readdir($dir)) {
$files[] = $f;
}
closedir($dir);
sort($files);
foreach($files as $filename) {
$full_path = "$base/$_sub_dir/$filename";
if(is_link($full_path)) {
// ignore
}
else if(is_dir($full_path)) {
if($filename == "." || $filename == "..") {
$file_list = array_merge(
$file_list,
list_files($base, "$_sub_dir/$filename")
);
}
}
else {
$full_path = str_replace("//", "/", $full_path);
$file_list[] = $full_path;
}
}
return $file_list;
}
function path_to_tags($path) {
$matches = array();
if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", basename($path), $matches)) {
$tags = $matches[1];
}
else {
$tags = dirname($path);
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = trim($tags);
}
return $tags;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Event API *

View File

@ -21,7 +21,10 @@ class BulkAdd extends Extension {
if($event->page_matches("bulk_add")) {
if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) {
set_time_limit(0);
$this->add_dir($_POST['dir']);
$list = add_dir($_POST['dir']);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
}
$this->theme->display_upload_results($page);
}
}
@ -34,7 +37,10 @@ class BulkAdd extends Extension {
}
if($event->cmd == "bulk-add") {
if(count($event->args) == 1) {
$this->add_dir($event->args[0]);
$list = add_dir($event->args[0]);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
}
}
}
}
@ -42,73 +48,5 @@ class BulkAdd extends Extension {
public function onAdminBuilding(AdminBuildingEvent $event) {
$this->theme->display_admin_block();
}
/**
* Generate the necessary DataUploadEvent for a given image and tags.
*/
private function add_image($tmpname, $filename, $tags) {
assert(file_exists($tmpname));
$pathinfo = pathinfo($filename);
if(!array_key_exists('extension', $pathinfo)) {
throw new UploadException("File has no extension");
}
$metadata = array();
$metadata['filename'] = $pathinfo['basename'];
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = $tags;
$metadata['source'] = null;
$event = new DataUploadEvent($tmpname, $metadata);
send_event($event);
if($event->image_id == -1) {
throw new UploadException("File type not recognised");
}
}
private function add_dir(/*string*/ $base, $subdir="") {
if(!is_dir($base)) {
$this->theme->add_status("Error", "$base is not a directory");
return;
}
$list = "";
foreach(glob("$base/$subdir/*") as $fullpath) {
$fullpath = str_replace("//", "/", $fullpath);
$shortpath = str_replace($base, "", $fullpath);
if(is_link($fullpath)) {
// ignore
}
else if(is_dir($fullpath)) {
$this->add_dir($base, str_replace($base, "", $fullpath));
}
else {
$pathinfo = pathinfo($fullpath);
$matches = array();
if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", $pathinfo["basename"], $matches)) {
$tags = $matches[1];
}
else {
$tags = $subdir;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$tags = trim($tags);
}
$list .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
try{
$this->add_image($fullpath, $pathinfo["basename"], $tags);
$list .= "ok\n";
}
catch(Exception $ex) {
$list .= "failed:<br>". $ex->getMessage();
}
}
}
if(strlen($list) > 0) {
$this->theme->add_status("Adding $subdir", $list);
}
}
}

View File

@ -33,7 +33,10 @@ class ArchiveFileHandler extends Extension {
$cmd = str_replace('%f', $event->tmpname, $cmd);
$cmd = str_replace('%d', $tmpdir, $cmd);
exec($cmd);
$this->add_dir($tmpdir);
$list = add_dir($tmpdir);
if(strlen($list) > 0) {
$this->theme->add_status("Adding files", $list);
}
deltree($tmpdir);
$event->image_id = -2; // default -1 = upload wasn't handled
}
@ -47,70 +50,4 @@ class ArchiveFileHandler extends Extension {
$exts = array("zip");
return in_array(strtolower($ext), $exts);
}
// copied from bulk add extension
private function add_image($tmpname, $filename, $tags) {
assert(file_exists($tmpname));
try {
$pathinfo = pathinfo($filename);
if(!array_key_exists('extension', $pathinfo)) {
throw new UploadException("File has no extension");
}
$metadata = array();
$metadata['filename'] = $pathinfo['basename'];
$metadata['extension'] = $pathinfo['extension'];
$metadata['tags'] = $tags;
$metadata['source'] = null;
$event = new DataUploadEvent($tmpname, $metadata);
send_event($event);
}
catch(UploadException $ex) {
return $ex->getMessage();
}
}
// copied from bulk add extension
private function add_dir($base, $subdir="") {
$list = "";
$dir = opendir("$base/$subdir");
$files = array();
while($f = readdir($dir)) {
$files[] = $f;
}
sort($files);
foreach($files as $filename) {
$fullpath = "$base/$subdir/$filename";
if(is_link($fullpath)) {
// ignore
}
else if(is_dir($fullpath)) {
if($filename[0] != ".") {
$this->add_dir($base, "$subdir/$filename");
}
}
else {
$tmpfile = $fullpath;
$tags = $subdir;
$tags = str_replace("/", " ", $tags);
$tags = str_replace("__", " ", $tags);
$list .= "<br>".html_escape("$subdir/$filename (".str_replace(" ", ",", $tags).")...");
$error = $this->add_image($tmpfile, $filename, $tags);
if(is_null($error)) {
$list .= "ok\n";
}
else {
$list .= "failed: $error\n";
}
}
}
closedir($dir);
// $this->theme->add_status("Adding $subdir", $list);
}
}

View File

@ -27,7 +27,8 @@ class PixelFileHandlerTheme extends Themelet {
}
}
$html = "<img alt='main image' class='shm-main-image' id='main_image' src='$u_ilink' data-width='{$image->width}' data-height='{$image->height}'>";
$html = "<img alt='main image' class='shm-main-image' id='main_image' src='$u_ilink' ".
"data-width='{$image->width}' data-height='{$image->height}'>";
$page->add_block(new Block("Image", $html, "main", 10));
}
}

View File

@ -384,7 +384,6 @@ class ImageIO extends Extension {
*/
private function send_file($image_id, $type) {
global $config;
global $database;
$image = Image::by_id($image_id);
global $page;