function init($height)
 {
     $this->candidates = array();
     foreach (ImageStdParams::get_defined_type_map() as $params) {
         if ($params->max_height() < $height || $params->sizing->max_crop) {
             continue;
         }
         if ($params->max_height() > 3 * $height) {
             break;
         }
         $this->candidates[] = $params;
     }
     $this->default = ImageStdParams::get_custom($height * 3, $height, 1, 0, $height);
     $this->height = $height;
 }
Пример #2
0
         $template->append('display', array($checkbox => $conf[$checkbox]), true);
     }
     $template->append('display', array('picture_informations' => unserialize($conf['picture_informations']), 'NB_CATEGORIES_PAGE' => $conf['nb_categories_page']), true);
     break;
 case 'sizes':
     // we only load the derivatives if it was not already loaded: it occurs
     // when submitting the form and an error remains
     if (!isset($page['sizes_loaded_in_tpl'])) {
         $is_gd = pwg_image::get_library() == 'gd' ? true : false;
         $template->assign('is_gd', $is_gd);
         $template->assign('sizes', array('original_resize_maxwidth' => $conf['original_resize_maxwidth'], 'original_resize_maxheight' => $conf['original_resize_maxheight'], 'original_resize_quality' => $conf['original_resize_quality']));
         foreach ($sizes_checkboxes as $checkbox) {
             $template->append('sizes', array($checkbox => $conf[$checkbox]), true);
         }
         // derivatives = multiple size
         $enabled = ImageStdParams::get_defined_type_map();
         $disabled = @unserialize(@$conf['disabled_derivatives']);
         if ($disabled === false) {
             $disabled = array();
         }
         $tpl_vars = array();
         foreach (ImageStdParams::get_all_types() as $type) {
             $tpl_var = array();
             $tpl_var['must_square'] = $type == IMG_SQUARE ? true : false;
             $tpl_var['must_enable'] = $type == IMG_SQUARE || $type == IMG_THUMB || $type == $conf['derivative_default_size'] ? true : false;
             if ($params = @$enabled[$type]) {
                 $tpl_var['enabled'] = true;
             } else {
                 $tpl_var['enabled'] = false;
                 $params = @$disabled[$type];
             }
Пример #3
0
/**
 * API method
 * Returns a list of missing derivatives (not generated yet)
 * @param mixed[] $params
 *    @option string types (optional)
 *    @option int[] ids
 *    @option int max_urls
 *    @option int prev_page (optional)
 */
function ws_getMissingDerivatives($params, &$service)
{
    global $conf;
    if (empty($params['types'])) {
        $types = array_keys(ImageStdParams::get_defined_type_map());
    } else {
        $types = array_intersect(array_keys(ImageStdParams::get_defined_type_map()), $params['types']);
        if (count($types) == 0) {
            return new PwgError(WS_ERR_INVALID_PARAM, "Invalid types");
        }
    }
    $max_urls = $params['max_urls'];
    $query = 'SELECT MAX(id)+1, COUNT(*) FROM ' . IMAGES_TABLE . ';';
    list($max_id, $image_count) = pwg_db_fetch_row(pwg_query($query));
    if (0 == $image_count) {
        return array();
    }
    $start_id = $params['prev_page'];
    if ($start_id <= 0) {
        $start_id = $max_id;
    }
    $uid = '&b=' . time();
    $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true;
    $conf['derivative_url_style'] = 2;
    //script
    $qlimit = min(5000, ceil(max($image_count / 500, $max_urls / count($types))));
    $where_clauses = ws_std_image_sql_filter($params, '');
    $where_clauses[] = 'id<start_id';
    if (!empty($params['ids'])) {
        $where_clauses[] = 'id IN (' . implode(',', $params['ids']) . ')';
    }
    $query_model = '
SELECT id, path, representative_ext, width, height, rotation
  FROM ' . IMAGES_TABLE . '
  WHERE ' . implode(' AND ', $where_clauses) . '
  ORDER BY id DESC
  LIMIT ' . $qlimit . '
;';
    $urls = array();
    do {
        $result = pwg_query(str_replace('start_id', $start_id, $query_model));
        $is_last = pwg_db_num_rows($result) < $qlimit;
        while ($row = pwg_db_fetch_assoc($result)) {
            $start_id = $row['id'];
            $src_image = new SrcImage($row);
            if ($src_image->is_mimetype()) {
                continue;
            }
            foreach ($types as $type) {
                $derivative = new DerivativeImage($type, $src_image);
                if ($type != $derivative->get_type()) {
                    continue;
                }
                if (@filemtime($derivative->get_path()) === false) {
                    $urls[] = $derivative->get_url() . $uid;
                }
            }
            if (count($urls) >= $max_urls and !$is_last) {
                break;
            }
        }
        if ($is_last) {
            $start_id = 0;
        }
    } while (count($urls) < $max_urls and $start_id);
    $ret = array();
    if ($start_id) {
        $ret['next_page'] = $start_id;
    }
    $ret['urls'] = $urls;
    return $ret;
}
Пример #4
0
function try_switch_source(DerivativeParams $params, $original_mtime)
{
    global $page;
    if (!isset($page['original_size'])) {
        return false;
    }
    $original_size = $page['original_size'];
    if ($page['rotation_angle'] == 90 || $page['rotation_angle'] == 270) {
        $tmp = $original_size[0];
        $original_size[0] = $original_size[1];
        $original_size[1] = $tmp;
    }
    $dsize = $params->compute_final_size($original_size);
    $use_watermark = $params->use_watermark;
    if ($use_watermark) {
        $use_watermark = $params->will_watermark($dsize);
    }
    $candidates = array();
    foreach (ImageStdParams::get_defined_type_map() as $candidate) {
        if ($candidate->type == $params->type) {
            continue;
        }
        if ($candidate->use_watermark != $use_watermark) {
            continue;
        }
        if ($candidate->max_width() < $params->max_width() || $candidate->max_height() < $params->max_height()) {
            continue;
        }
        $candidate_size = $candidate->compute_final_size($original_size);
        if ($dsize != $params->compute_final_size($candidate_size)) {
            continue;
        }
        if ($params->sizing->max_crop == 0) {
            if ($candidate->sizing->max_crop != 0) {
                continue;
            }
        } else {
            if ($use_watermark && $candidate->use_watermark) {
                continue;
            }
            //a square that requires watermark should not be generated from a larger derivative with watermark, because if the watermark is not centered on the large image, it will be cropped.
            if ($candidate->sizing->max_crop != 0) {
                continue;
            }
            // this could be optimized
            if ($candidate_size[0] < $params->sizing->min_size[0] || $candidate_size[1] < $params->sizing->min_size[1]) {
                continue;
            }
        }
        $candidates[] = $candidate;
    }
    foreach (array_reverse($candidates) as $candidate) {
        $candidate_path = $page['derivative_path'];
        $candidate_path = str_replace('-' . derivative_to_url($params->type), '-' . derivative_to_url($candidate->type), $candidate_path);
        $candidate_mtime = @filemtime($candidate_path);
        if ($candidate_mtime === false || $candidate_mtime < $original_mtime || $candidate_mtime < $candidate->last_mod_time) {
            continue;
        }
        $params->use_watermark = false;
        $params->sharpen = min(1, $params->sharpen);
        $page['src_path'] = $candidate_path;
        $page['src_url'] = $page['root_path'] . substr($candidate_path, strlen(PHPWG_ROOT_PATH));
        $page['rotation_angle'] = 0;
        return true;
    }
    return false;
}
Пример #5
0
 /**
  * @todo : documentation of DerivativeImage::build
  */
 private static function build($src, &$params, &$rel_path, &$rel_url, &$is_cached = null)
 {
     if ($src->has_size() && $params->is_identity($src->get_size())) {
         // the source image is smaller than what we should do - we do not upsample
         if (!$params->will_watermark($src->get_size()) && !$src->rotation) {
             // no watermark, no rotation required -> we will use the source image
             $params = null;
             $rel_path = $rel_url = $src->rel_path;
             return;
         }
         $defined_types = array_keys(ImageStdParams::get_defined_type_map());
         for ($i = 0; $i < count($defined_types); $i++) {
             if ($defined_types[$i] == $params->type) {
                 for ($i--; $i >= 0; $i--) {
                     $smaller = ImageStdParams::get_by_type($defined_types[$i]);
                     if ($smaller->sizing->max_crop == $params->sizing->max_crop && $smaller->is_identity($src->get_size())) {
                         $params = $smaller;
                         self::build($src, $params, $rel_path, $rel_url, $is_cached);
                         return;
                     }
                 }
                 break;
             }
         }
     }
     $tokens = array();
     $tokens[] = substr($params->type, 0, 2);
     if ($params->type == IMG_CUSTOM) {
         $params->add_url_tokens($tokens);
     }
     $loc = $src->rel_path;
     if (substr_compare($loc, './', 0, 2) == 0) {
         $loc = substr($loc, 2);
     } elseif (substr_compare($loc, '../', 0, 3) == 0) {
         $loc = substr($loc, 3);
     }
     $loc = substr_replace($loc, '-' . implode('_', $tokens), strrpos($loc, '.'), 0);
     $rel_path = PWG_DERIVATIVE_DIR . $loc;
     global $conf;
     $url_style = $conf['derivative_url_style'];
     if (!$url_style) {
         $mtime = @filemtime(PHPWG_ROOT_PATH . $rel_path);
         if ($mtime === false or $mtime < $params->last_mod_time) {
             $is_cached = false;
             $url_style = 2;
         } else {
             $url_style = 1;
         }
     }
     if ($url_style == 2) {
         $rel_url = 'i';
         if ($conf['php_extension_in_urls']) {
             $rel_url .= '.php';
         }
         if ($conf['question_mark_in_urls']) {
             $rel_url .= '?';
         }
         $rel_url .= '/' . $loc;
     } else {
         $rel_url = $rel_path;
     }
 }
Пример #6
0
    $my_conf = array_intersect_key($my_conf, $default_conf);
    conf_update_param('modus_theme', addslashes(serialize($my_conf)));
    global $page;
    $page['infos'][] = l10n('Information data registered in database');
}
// *************** template init ********************
foreach ($text_values as $k) {
    $template->assign(strtoupper($k), $my_conf[$k]);
}
foreach ($bool_values as $k) {
    $template->assign(strtoupper($k), $my_conf[$k]);
}
// we don't use square thumbs if the thumb size is 0
$template->assign('use_album_square_thumbs', 0 != $my_conf['album_thumb_size']);
if (0 == $my_conf['album_thumb_size']) {
    $template->assign('ALBUM_THUMB_SIZE', 250);
}
$available_derivatives = array();
foreach (array_keys(ImageStdParams::get_defined_type_map()) as $type) {
    $available_derivatives[$type] = l10n($type);
}
$available_skins = array();
$skin_dir = dirname(dirname(__FILE__)) . '/skins/';
$skin_suffix = '.inc.php';
foreach (glob($skin_dir . '*' . $skin_suffix) as $file) {
    $skin = substr($file, strlen($skin_dir), -strlen($skin_suffix));
    $available_skins[$skin] = ucwords(str_replace('_', ' ', $skin));
}
$template->assign(array('available_derivatives' => $available_derivatives, 'available_skins' => $available_skins));
$template->set_filename('modus_content', dirname(__FILE__) . '/modus_admin.tpl');
$template->assign_var_from_handle('ADMIN_CONTENT', 'modus_content');
if (count($errors) == 0) {
    $watermark = new WatermarkParams();
    $watermark->file = $pwatermark['file'];
    $watermark->xpos = intval($pwatermark['xpos']);
    $watermark->ypos = intval($pwatermark['ypos']);
    $watermark->xrepeat = intval($pwatermark['xrepeat']);
    $watermark->yrepeat = intval($pwatermark['yrepeat']);
    $watermark->opacity = intval($pwatermark['opacity']);
    $watermark->min_size = array(intval($pwatermark['minw']), intval($pwatermark['minh']));
    $old_watermark = ImageStdParams::get_watermark();
    $watermark_changed = $watermark->file != $old_watermark->file || $watermark->xpos != $old_watermark->xpos || $watermark->ypos != $old_watermark->ypos || $watermark->xrepeat != $old_watermark->xrepeat || $watermark->yrepeat != $old_watermark->yrepeat || $watermark->opacity != $old_watermark->opacity;
    // save the new watermark configuration
    ImageStdParams::set_watermark($watermark);
    // do we have to regenerate the derivatives (and which types)?
    $changed_types = array();
    foreach (ImageStdParams::get_defined_type_map() as $type => $params) {
        $old_use_watermark = $params->use_watermark;
        ImageStdParams::apply_global($params);
        $changed = $params->use_watermark != $old_use_watermark;
        if (!$changed and $params->use_watermark) {
            $changed = $watermark_changed;
        }
        if (!$changed and $params->use_watermark) {
            // if thresholds change and before/after the threshold is lower than the corresponding derivative side -> some derivatives might switch the watermark
            $changed |= $watermark->min_size[0] != $old_watermark->min_size[0] and ($watermark->min_size[0] < $params->max_width() or $old_watermark->min_size[0] < $params->max_width());
            $changed |= $watermark->min_size[1] != $old_watermark->min_size[1] and ($watermark->min_size[1] < $params->max_height() or $old_watermark->min_size[1] < $params->max_height());
        }
        if ($changed) {
            $params->last_mod_time = time();
            $changed_types[] = $type;
        }
Пример #8
0
function default_picture_content($content, $element_info)
{
    global $conf;
    if (!empty($content)) {
        // someone hooked us - so we skip;
        return $content;
    }
    if (isset($_COOKIE['picture_deriv'])) {
        if (array_key_exists($_COOKIE['picture_deriv'], ImageStdParams::get_defined_type_map())) {
            pwg_set_session_var('picture_deriv', $_COOKIE['picture_deriv']);
        }
        setcookie('picture_deriv', false, 0, cookie_path());
    }
    $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
    $selected_derivative = $element_info['derivatives'][$deriv_type];
    $unique_derivatives = array();
    $show_original = isset($element_info['element_url']);
    $added = array();
    foreach ($element_info['derivatives'] as $type => $derivative) {
        if ($type == IMG_SQUARE || $type == IMG_THUMB) {
            continue;
        }
        if (!array_key_exists($type, ImageStdParams::get_defined_type_map())) {
            continue;
        }
        $url = $derivative->get_url();
        if (isset($added[$url])) {
            continue;
        }
        $added[$url] = 1;
        $show_original &= !$derivative->same_as_source();
        $unique_derivatives[$type] = $derivative;
    }
    global $page, $template;
    if ($show_original) {
        $template->assign('U_ORIGINAL', $element_info['element_url']);
    }
    $template->append('current', array('selected_derivative' => $selected_derivative, 'unique_derivatives' => $unique_derivatives), true);
    $template->set_filenames(array('default_content' => 'picture_content.tpl'));
    $template->assign(array('ALT_IMG' => $element_info['file'], 'COOKIE_PATH' => cookie_path()));
    return $template->parse('default_content', true);
}
Пример #9
0
        $template->assign('CONTENT_DESCRIPTION', $page['comment']);
    }
    if (isset($page['category']['count_categories']) and $page['category']['count_categories'] == 0) {
        // count_categories might be computed by menubar - if the case unassign flat link if no sub albums
        $template->clear_assign('U_MODE_FLAT');
    }
    //------------------------------------------------------ main part : thumbnails
    if (0 == $page['start'] and !isset($page['flat']) and !isset($page['chronology_field']) and ('recent_cats' == $page['section'] or 'categories' == $page['section']) and (!isset($page['category']['count_categories']) or $page['category']['count_categories'] > 0)) {
        include PHPWG_ROOT_PATH . 'include/category_cats.inc.php';
    }
    if (!empty($page['items'])) {
        include PHPWG_ROOT_PATH . 'include/category_default.inc.php';
        $url = add_url_params(duplicate_index_url(), array('display' => ''));
        $selected_type = $template->get_template_vars('derivative_params')->type;
        $template->clear_assign('derivative_params');
        $type_map = ImageStdParams::get_defined_type_map();
        unset($type_map[IMG_XXLARGE], $type_map[IMG_XLARGE]);
        foreach ($type_map as $params) {
            $template->append('image_derivatives', array('DISPLAY' => l10n($params->type), 'URL' => $url . $params->type, 'SELECTED' => $params->type == $selected_type ? true : false));
        }
    }
    // slideshow
    // execute after init thumbs in order to have all picture informations
    if (!empty($page['cat_slideshow_url'])) {
        if (isset($_GET['slideshow'])) {
            redirect($page['cat_slideshow_url']);
        } elseif ($conf['index_slideshow_icon']) {
            $template->assign('U_SLIDESHOW', $page['cat_slideshow_url']);
        }
    }
}
Пример #10
0
function Fotorama_end_picture()
{
    global $template, $conf, $user, $page;
    if (Fotorama_is_replace_picture()) {
        $url_up = duplicate_index_url(array('start' => floor($page['current_rank'] / $page['nb_image_page']) * $page['nb_image_page']), array('start'));
        //slideshow end
        $template->assign(array('U_SLIDESHOW_STOP' => $url_up));
        $template->assign('replace_picture', true);
    }
    if (!$page['slideshow']) {
        return;
    }
    load_language('plugin.lang', FOTORAMA_PATH);
    $split_limit = 400;
    if ('mobile' == get_device()) {
        $split_limit /= 2;
    }
    $view_offset = null;
    if (count($page['items']) >= 1.2 * $split_limit) {
        $first = $split_limit * 0.2;
        $last = $split_limit - $first;
        $first = $page['current_rank'] - $first;
        if ($first < 0) {
            $first += count($page['items']);
        }
        $last = $page['current_rank'] + $last;
        if ($last >= count($page['items'])) {
            $last -= count($page['items']);
        }
        if ($first < $last) {
            $selection = array_slice($page['items'], $first, $last - $first);
            $view_borders = array(0, count($selection) - 1);
            $view_offset = array('from' => 0, 'offset' => $first);
        } else {
            $selection = array_slice($page['items'], 0, $last);
            $view_borders = array(count($selection), count($selection) - 1);
            $view_offset = array('from' => count($selection), 'offset' => $first - count($selection));
            $selection = array_merge($selection, array_slice($page['items'], $first));
        }
    } else {
        $selection = $page['items'];
        $view_borders = null;
    }
    $query = '
  SELECT *
    FROM ' . IMAGES_TABLE . '
    WHERE id IN (' . implode(',', $selection) . ')
    ORDER BY FIELD(id, ' . implode(',', $selection) . ')
  ;';
    $result = pwg_query($query);
    $current = $template->get_template_vars('current');
    if (isset($current['selected_derivative'])) {
        $type = $current['selected_derivative']->get_type();
    }
    $defined = ImageStdParams::get_defined_type_map();
    if (!isset($type) or !isset($defined[$type])) {
        $type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
    }
    $skip = -1;
    $big_type = $type;
    $next_type = $type;
    foreach (ImageStdParams::get_defined_type_map() as $def_type => $params) {
        if ($def_type == $type) {
            $skip = 2;
        }
        if ($skip >= 0) {
            $big_type = $def_type;
        }
        if ($skip >= 1 and $conf['Fotorama']['resize']) {
            $next_type = $def_type;
        }
        if ($skip == 0) {
            break;
        }
        $skip = $skip - 1;
    }
    $type = $next_type;
    // +1 size for inpage slideshow
    if ($conf['Fotorama']['only_fullscreen']) {
        $type = $big_type;
    }
    $type_params = ImageStdParams::get_by_type($type);
    $big_type_params = ImageStdParams::get_by_type($big_type);
    if ($conf['Fotorama']['nav'] == 'thumbs' or $conf['Fotorama']['fullscreen_nav'] == 'thumbs') {
        $has_thumbs = true;
    } else {
        $has_thumbs = false;
    }
    if ($has_thumbs) {
        if ($conf['Fotorama']['square_thumb']) {
            $thumb_params = ImageStdParams::get_custom($conf['Fotorama']['thumbheight'], $conf['Fotorama']['thumbheight'], 1, $conf['Fotorama']['thumbheight'], $conf['Fotorama']['thumbheight']);
        } else {
            $thumb_params = ImageStdParams::get_custom(9999, $conf['Fotorama']['thumbheight']);
        }
    }
    $picture = array();
    while ($row = pwg_db_fetch_assoc($result)) {
        $row['src_image'] = new SrcImage($row);
        $row['derivative'] = new DerivativeImage($type_params, $row['src_image']);
        $row['derivative_big'] = new DerivativeImage($big_type_params, $row['src_image']);
        if ($has_thumbs) {
            $row['derivative_thumb'] = new DerivativeImage($thumb_params, $row['src_image']);
        }
        $row['url'] = duplicate_picture_url(array('image_id' => $row['id'], 'image_file' => $row['file']), array('start'));
        $row['TITLE'] = render_element_name($row);
        $picture[] = $row;
    }
    $picture = trigger_change('fotorama_items', $picture, $selection);
    $template->assign(array('TOTAL_ITEMS' => count($page['items']), 'view_borders' => $view_borders, 'view_offset' => $view_offset, 'current_rank' => array_search($page['image_id'], $selection)));
    $template->assign('item_height', ImageStdParams::get_by_type($type)->max_height());
    $template->assign('items', $picture);
    $template->assign(array('Fotorama' => $conf['Fotorama']));
    $template->assign('Fotorama_has_thumbs', $has_thumbs);
    if (is_file('./themes/' . $user['theme'] . '/template/fotorama.tpl')) {
        $template->set_filenames(array('slideshow' => realpath('./themes/' . $user['theme'] . '/template/fotorama.tpl')));
    } else {
        $template->set_filenames(array('slideshow' => realpath(FOTORAMA_PATH . 'template/fotorama.tpl')));
    }
    $template->assign('FOTORAMA_CONTENT_PATH', realpath(FOTORAMA_PATH . 'template/fotorama-content.tpl'));
}
Пример #11
0
function modus_picture_content($content, $element_info)
{
    global $conf, $picture, $template;
    if (!empty($content)) {
        // someone hooked us - so we skip;
        return $content;
    }
    $unique_derivatives = array();
    $show_original = isset($element_info['element_url']);
    $added = array();
    foreach ($element_info['derivatives'] as $type => $derivative) {
        if ($type == IMG_SQUARE || $type == IMG_THUMB) {
            continue;
        }
        if (!array_key_exists($type, ImageStdParams::get_defined_type_map())) {
            continue;
        }
        $url = $derivative->get_url();
        if (isset($added[$url])) {
            continue;
        }
        $added[$url] = 1;
        $show_original &= !$derivative->same_as_source();
        $unique_derivatives[$type] = $derivative;
    }
    if (isset($_COOKIE['picture_deriv'])) {
        // ignore persistence
        setcookie('picture_deriv', false, 0, cookie_path());
    }
    $selected_derivative = null;
    if (isset($_COOKIE['phavsz'])) {
        $available_size = explode('x', $_COOKIE['phavsz']);
    } elseif (($caps = pwg_get_session_var('caps')) && $caps[0] > 1) {
        $available_size = array($caps[0] * $caps[1], $caps[0] * ($caps[2] - 100), $caps[0]);
    }
    if (isset($available_size)) {
        foreach ($unique_derivatives as $derivative) {
            $size = $derivative->get_size();
            if (!$size) {
                break;
            }
            if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1]) {
                $selected_derivative = $derivative;
            } else {
                if ($available_size[2] > 1 || !$selected_derivative) {
                    $selected_derivative = $derivative;
                }
                break;
            }
        }
        if ($available_size[2] > 1 && $selected_derivative) {
            $ratio_w = $size[0] / $available_size[0];
            $ratio_h = $size[1] / $available_size[1];
            if ($ratio_w > 1 || $ratio_h > 1) {
                if ($ratio_w > $ratio_h) {
                    $display_size = array($available_size[0] / $available_size[2], floor($size[1] / $ratio_w / $available_size[2]));
                } else {
                    $display_size = array(floor($size[0] / $ratio_h / $available_size[2]), $available_size[1] / $available_size[2]);
                }
            } else {
                $display_size = array(round($size[0] / $available_size[2]), round($size[1] / $available_size[2]));
            }
            $template->assign(array('rvas_display_size' => $display_size, 'rvas_natural_size' => $size));
        }
        if (isset($picture['next']) and $picture['next']['src_image']->is_original()) {
            $next_best = null;
            foreach ($picture['next']['derivatives'] as $derivative) {
                $size = $derivative->get_size();
                if (!$size) {
                    break;
                }
                if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1]) {
                    $next_best = $derivative;
                } else {
                    if ($available_size[2] > 1 || !$next_best) {
                        $next_best = $derivative;
                    }
                    break;
                }
            }
            if (isset($next_best)) {
                $template->assign('U_PREFETCH', $next_best->get_url());
            }
        }
    }
    $as_pending = false;
    if (!$selected_derivative) {
        $as_pending = true;
        $selected_derivative = $element_info['derivatives'][pwg_get_session_var('picture_deriv', $conf['derivative_default_size'])];
    }
    if ($show_original) {
        $template->assign('U_ORIGINAL', $element_info['element_url']);
    }
    $template->append('current', array('selected_derivative' => $selected_derivative, 'unique_derivatives' => $unique_derivatives), true);
    $template->set_filenames(array('default_content' => 'picture_content_asize.tpl'));
    $template->assign(array('ALT_IMG' => $element_info['file'], 'COOKIE_PATH' => cookie_path(), 'RVAS_PENDING' => $as_pending));
    return $template->parse('default_content', true);
}
Пример #12
0
/**
 * API method
 * Returns a list of categories
 * @param mixed[] $params
 *    @option int cat_id (optional)
 *    @option bool recursive
 *    @option bool public
 *    @option bool tree_output
 *    @option bool fullname
 */
function ws_categories_getList($params, &$service)
{
    global $user, $conf;
    if (!in_array($params['thumbnail_size'], array_keys(ImageStdParams::get_defined_type_map()))) {
        return new PwgError(WS_ERR_INVALID_PARAM, "Invalid thumbnail_size");
    }
    $where = array('1=1');
    $join_type = 'INNER';
    $join_user = $user['id'];
    if (!$params['recursive']) {
        if ($params['cat_id'] > 0) {
            $where[] = '(
        id_uppercat = ' . (int) $params['cat_id'] . '
        OR id=' . (int) $params['cat_id'] . '
      )';
        } else {
            $where[] = 'id_uppercat IS NULL';
        }
    } else {
        if ($params['cat_id'] > 0) {
            $where[] = 'uppercats ' . DB_REGEX_OPERATOR . ' \'(^|,)' . (int) $params['cat_id'] . '(,|$)\'';
        }
    }
    if ($params['public']) {
        $where[] = 'status = "public"';
        $where[] = 'visible = "true"';
        $join_user = $conf['guest_id'];
    } else {
        if (is_admin()) {
            // in this very specific case, we don't want to hide empty
            // categories. Function calculate_permissions will only return
            // categories that are either locked or private and not permitted
            //
            // calculate_permissions does not consider empty categories as forbidden
            $forbidden_categories = calculate_permissions($user['id'], $user['status']);
            $where[] = 'id NOT IN (' . $forbidden_categories . ')';
            $join_type = 'LEFT';
        }
    }
    $query = '
SELECT
    id, name, comment, permalink,
    uppercats, global_rank, id_uppercat,
    nb_images, count_images AS total_nb_images,
    representative_picture_id, user_representative_picture_id, count_images, count_categories,
    date_last, max_date_last, count_categories AS nb_categories
  FROM ' . CATEGORIES_TABLE . '
    ' . $join_type . ' JOIN ' . USER_CACHE_CATEGORIES_TABLE . '
    ON id=cat_id AND user_id=' . $join_user . '
  WHERE ' . implode("\n    AND ", $where) . '
;';
    $result = pwg_query($query);
    // management of the album thumbnail -- starts here
    $image_ids = array();
    $categories = array();
    $user_representative_updates_for = array();
    // management of the album thumbnail -- stops here
    $cats = array();
    while ($row = pwg_db_fetch_assoc($result)) {
        $row['url'] = make_index_url(array('category' => $row));
        foreach (array('id', 'nb_images', 'total_nb_images', 'nb_categories') as $key) {
            $row[$key] = (int) $row[$key];
        }
        if ($params['fullname']) {
            $row['name'] = strip_tags(get_cat_display_name_cache($row['uppercats'], null));
        } else {
            $row['name'] = strip_tags(trigger_change('render_category_name', $row['name'], 'ws_categories_getList'));
        }
        $row['comment'] = strip_tags(trigger_change('render_category_description', $row['comment'], 'ws_categories_getList'));
        // management of the album thumbnail -- starts here
        //
        // on branch 2.3, the algorithm is duplicated from
        // include/category_cats, but we should use a common code for Piwigo 2.4
        //
        // warning : if the API method is called with $params['public'], the
        // album thumbnail may be not accurate. The thumbnail can be viewed by
        // the connected user, but maybe not by the guest. Changing the
        // filtering method would be too complicated for now. We will simply
        // avoid to persist the user_representative_picture_id in the database
        // if $params['public']
        if (!empty($row['user_representative_picture_id'])) {
            $image_id = $row['user_representative_picture_id'];
        } else {
            if (!empty($row['representative_picture_id'])) {
                // if a representative picture is set, it has priority
                $image_id = $row['representative_picture_id'];
            } else {
                if ($conf['allow_random_representative']) {
                    // searching a random representant among elements in sub-categories
                    $image_id = get_random_image_in_category($row);
                } else {
                    // searching a random representant among representant of sub-categories
                    if ($row['count_categories'] > 0 and $row['count_images'] > 0) {
                        $query = '
SELECT representative_picture_id
  FROM ' . CATEGORIES_TABLE . '
    INNER JOIN ' . USER_CACHE_CATEGORIES_TABLE . '
    ON id=cat_id AND user_id=' . $user['id'] . '
  WHERE uppercats LIKE \'' . $row['uppercats'] . ',%\'
    AND representative_picture_id IS NOT NULL
        ' . get_sql_condition_FandF(array('visible_categories' => 'id'), "\n  AND") . '
  ORDER BY ' . DB_RANDOM_FUNCTION . '()
  LIMIT 1
;';
                        $subresult = pwg_query($query);
                        if (pwg_db_num_rows($subresult) > 0) {
                            list($image_id) = pwg_db_fetch_row($subresult);
                        }
                    }
                }
            }
        }
        if (isset($image_id)) {
            if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id) {
                $user_representative_updates_for[$row['id']] = $image_id;
            }
            $row['representative_picture_id'] = $image_id;
            $image_ids[] = $image_id;
            $categories[] = $row;
        }
        unset($image_id);
        // management of the album thumbnail -- stops here
        $cats[] = $row;
    }
    usort($cats, 'global_rank_compare');
    // management of the album thumbnail -- starts here
    if (count($categories) > 0) {
        $thumbnail_src_of = array();
        $new_image_ids = array();
        $query = '
SELECT id, path, representative_ext, level
  FROM ' . IMAGES_TABLE . '
  WHERE id IN (' . implode(',', $image_ids) . ')
;';
        $result = pwg_query($query);
        while ($row = pwg_db_fetch_assoc($result)) {
            if ($row['level'] <= $user['level']) {
                $thumbnail_src_of[$row['id']] = DerivativeImage::url($params['thumbnail_size'], $row);
            } else {
                // problem: we must not display the thumbnail of a photo which has a
                // higher privacy level than user privacy level
                //
                // * what is the represented category?
                // * find a random photo matching user permissions
                // * register it at user_representative_picture_id
                // * set it as the representative_picture_id for the category
                foreach ($categories as &$category) {
                    if ($row['id'] == $category['representative_picture_id']) {
                        // searching a random representant among elements in sub-categories
                        $image_id = get_random_image_in_category($category);
                        if (isset($image_id) and !in_array($image_id, $image_ids)) {
                            $new_image_ids[] = $image_id;
                        }
                        if ($conf['representative_cache_on_level']) {
                            $user_representative_updates_for[$category['id']] = $image_id;
                        }
                        $category['representative_picture_id'] = $image_id;
                    }
                }
                unset($category);
            }
        }
        if (count($new_image_ids) > 0) {
            $query = '
SELECT id, path, representative_ext
  FROM ' . IMAGES_TABLE . '
  WHERE id IN (' . implode(',', $new_image_ids) . ')
;';
            $result = pwg_query($query);
            while ($row = pwg_db_fetch_assoc($result)) {
                $thumbnail_src_of[$row['id']] = DerivativeImage::url($params['thumbnail_size'], $row);
            }
        }
    }
    // compared to code in include/category_cats, we only persist the new
    // user_representative if we have used $user['id'] and not the guest id,
    // or else the real guest may see thumbnail that he should not
    if (!$params['public'] and count($user_representative_updates_for)) {
        $updates = array();
        foreach ($user_representative_updates_for as $cat_id => $image_id) {
            $updates[] = array('user_id' => $user['id'], 'cat_id' => $cat_id, 'user_representative_picture_id' => $image_id);
        }
        mass_updates(USER_CACHE_CATEGORIES_TABLE, array('primary' => array('user_id', 'cat_id'), 'update' => array('user_representative_picture_id')), $updates);
    }
    foreach ($cats as &$cat) {
        foreach ($categories as $category) {
            if ($category['id'] == $cat['id'] and isset($category['representative_picture_id'])) {
                $cat['tn_url'] = $thumbnail_src_of[$category['representative_picture_id']];
            }
        }
        // we don't want them in the output
        unset($cat['user_representative_picture_id'], $cat['count_images'], $cat['count_categories']);
    }
    unset($cat);
    // management of the album thumbnail -- stops here
    if ($params['tree_output']) {
        return categories_flatlist_to_tree($cats);
    }
    return array('categories' => new PwgNamedArray($cats, 'category', ws_std_get_category_xml_attributes()));
}