1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 
<?php
$plugin_is_filter = 5 | FEATURE_PLUGIN;
$plugin_description = gettext('Support for <em>favorites</em> handling.');
$plugin_author = "Stephen Billard (sbillard)";
$plugin_category = gettext('Media');
$option_interface = 'favoritesOptions';
require_once(SERVERPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/favoritesHandler/class-favorites.php');
class favoritesOptions {
    function __construct() {
        if (OFFSET_PATH == 2) {
            setOptionDefault('favorites_multi', 0);
            setOptionDefault('favorites_link', '_PAGE_/favorites');
            gettext($str = 'My favorites');
            setOptionDefault('favorites_title', getAllTranslations($str));
            setOptionDefault('favorites_linktext', getAllTranslations($str));
            gettext($str = 'The albums and images selected as favorites.');
            setOptionDefault('favorites_desc', getAllTranslations($str));
            gettext($str = 'Add favorite');
            setOptionDefault('favorites_add_button', getAllTranslations($str));
            gettext($str = 'Remove favorite');
            setOptionDefault('favorites_remove_button', getAllTranslations($str));
            setOptionDefault('favorites_album_sort_type', 'title');
            setOptionDefault('favorites_image_sort_type', 'title');
            setOptionDefault('favorites_album_sort_direction', '');
            setOptionDefault('favorites_image_sort_direction', '');
        }
    }
    function getOptionsSupported() {
        global $_zp_gallery;
        $themename = $_zp_gallery->getCurrentTheme();
        $curdir = getcwd();
        $root = SERVERPATH . '/' . THEMEFOLDER . '/' . $themename . '/';
        chdir($root);
        $filelist = safe_glob('*.php');
        $list = array();
        foreach ($filelist as $file) {
            $file = filesystemToInternal($file);
            $list[$file] = str_replace('.php', '', $file);
        }
        $list = array_diff($list, standardScripts());
        $options = array(gettext('Link text')            => array('key'                  => 'favorites_linktext', 'type'                 => OPTION_TYPE_TEXTBOX,
                                        'multilingual' => true,
                                        'order'              => 2,
                                        'desc'               => gettext('The text for the link to the favorites page.')),
                        gettext('Multiple sets')     => array('key'      => 'favorites_multi', 'type'    => OPTION_TYPE_CHECKBOX,
                                        'order'  => 6,
                                        'desc'   => gettext('If enabled a user may have multiple (named) favorites.')),
                        gettext('Add button')            => array('key'                  => 'favorites_add_button', 'type'               => OPTION_TYPE_TEXTBOX,
                                        'multilingual' => true,
                                        'order'              => 6,
                                        'desc'               => gettext('Default text for the <em>add to favorites</em> button.')),
                        gettext('Remove button')     => array('key'                  => 'favorites_remove_button', 'type'                => OPTION_TYPE_TEXTBOX,
                                        'multilingual' => true,
                                        'order'              => 7,
                                        'desc'               => gettext('Default text for the <em>remove from favorites</em> button.')),
                        gettext('Title')                     => array('key'                  => 'favorites_title', 'type'                => OPTION_TYPE_TEXTBOX,
                                        'multilingual' => true,
                                        'order'              => 3,
                                        'desc'               => gettext('The favorites page title text.')),
                        gettext('Description')       => array('key'                  => 'favorites_desc', 'type'                 => OPTION_TYPE_TEXTAREA,
                                        'multilingual' => true,
                                        'order'              => 5,
                                        'desc'               => gettext('The favorites page description text.')),
                        gettext('Sort albums by')    => array('key'      => 'favorites_albumsort', 'type'    => OPTION_TYPE_CUSTOM,
                                        'order'  => 9,
                                        'desc'   => ''),
                        gettext('Sort images by')    => array('key'      => 'favorites_imagesort', 'type'    => OPTION_TYPE_CUSTOM,
                                        'order'  => 10,
                                        'desc'   => '')
        );
        if (!MOD_REWRITE) {
            $options['note'] = array(
                            'key'        => 'favorites_note',
                            'type'   => OPTION_TYPE_NOTE,
                            'order'  => 0,
                            'desc'   => gettext('<p class="notebox">Favorites requires the <code>mod_rewrite</code> option be enabled.</p>')
            );
        }
        return $options;
    }
    function handleOption($option, $currentValue) {
        $sort = array(gettext('Filename')    => 'filename',
                        gettext('Custom')        => 'custom',
                        gettext('Date')          => 'date',
                        gettext('Title')         => 'title',
                        gettext('ID')                => 'id',
                        gettext('Filemtime') => 'mtime',
                        gettext('Owner')         => 'owner',
                        gettext('Published') => 'show'
        );
        switch ($option) {
            case 'favorites_albumsort':
                ?>
                <span class="nowrap">
                    <select id="albumsortselect" name="subalbumsortby" onchange="update_direction(this, 'album_direction_div', 'album_custom_div');">
                        <?php
                        $cvt = $type = strtolower(getOption('favorites_album_sort_type'));
                        if ($type && !in_array($type, $sort)) {
                            $cv = array('custom');
                        } else {
                            $cv = array($type);
                        }
                        generateListFromArray($cv, $sort, false, true);
                        ?>
                    </select>
                    <?php
                    if (($type == 'random') || ($type == '')) {
                        $dsp = 'none';
                    } else {
                        $dsp = 'inline';
                    }
                    ?>
                    <label id="album_direction_div" style="display:<?php echo $dsp; ?>;white-space:nowrap;">
                        <?php echo gettext("Descending"); ?>
                        <input type="checkbox" name="album_sortdirection" value="1"
                        <?php
                        if (getOption('favorites_album_sort_direction')) {
                            echo "CHECKED";
                        };
                        ?> />
                    </label>
                </span>
                <?php
                break;
            case 'favorites_imagesort':
                ?>
                <span class="nowrap">
                    <select id="imagesortselect" name="sortby" onchange="update_direction(this, 'image_direction_div', 'image_custom_div')">
                        <?php
                        $cvt = $type = strtolower(getOption('favorites_image_sort_type'));
                        if ($type && !in_array($type, $sort)) {
                            $cv = array('custom');
                        } else {
                            $cv = array($type);
                        }
                        generateListFromArray($cv, $sort, false, true);
                        ?>
                    </select>
                    <?php
                    if (($type == 'random') || ($type == '')) {
                        $dsp = 'none';
                    } else {
                        $dsp = 'inline';
                    }
                    ?>
                    <label id="image_direction_div" style="display:<?php echo $dsp; ?>;white-space:nowrap;">
                        <?php echo gettext("Descending"); ?>
                        <input type="checkbox" name="image_sortdirection" value="1"
                        <?php
                        if (getOption('favorites_image_sort_direction')) {
                            echo ' checked="checked"';
                        }
                        ?> />
                    </label>
                </span>
                <?php
                break;
        }
    }
    function handleOptionSave($theme, $album) {
        $sorttype = strtolower(sanitize($_POST['sortby'], 3));
        if ($sorttype == 'custom') {
            $sorttype = unquote(strtolower(sanitize($_POST['customimagesort'], 3)));
        }
        setOption('favorites_image_sort_type', $sorttype);
        if (($sorttype == 'manual') || ($sorttype == 'random')) {
            setOption('favorites_image_sort_direction', 0);
        } else {
            if (empty($sorttype)) {
                $direction = 0;
            } else {
                $direction = isset($_POST['image_sortdirection']);
            }
            setOption('favorites_image_sort_direction', $direction ? 'DESC' : '');
        }
        $sorttype = strtolower(sanitize($_POST['subalbumsortby'], 3));
        if ($sorttype == 'custom')
            $sorttype = strtolower(sanitize($_POST['customalbumsort'], 3));
        setOption('favorites_album_sort_type', $sorttype);
        if (($sorttype == 'manual') || ($sorttype == 'random')) {
            $direction = 0;
        } else {
            $direction = isset($_POST['album_sortdirection']);
        }
        setOption('favorites_album_sort_direction', $direction ? 'DESC' : '');
        return false;
    }
}
$_zp_conf_vars['special_pages']['favorites'] = array(
        'define' => '_FAVORITES_',
        'rewrite' => getOption('favorites_link'),
        'option' => 'favorites_link',
        'default' => '_PAGE_/favorites');
$_zp_conf_vars['special_pages'][] = array(
        'definition' => '%FAVORITES%',
        'rewrite' => '_FAVORITES_');
$_zp_conf_vars['special_pages'][] = array(
        'define' => false,
        'rewrite' => '^%FAVORITES%/(.+)/([0-9]+)/?$',
        'rule' => '%REWRITE% index.php?p=favorites&instance=$1&page=$2 [L,QSA]');
$_zp_conf_vars['special_pages'][] = array(
        'define' => false,
        'rewrite' => '^%FAVORITES%/([0-9]+)/?$',
        'rule' => '%REWRITE% index.php?p=favorites&page=$1 [L,QSA]');
$_zp_conf_vars['special_pages'][] = array(
        'define' => false,
        'rewrite' => '^%FAVORITES%/(.+)/?$',
        'rule' => '%REWRITE% index.php?p=favorites&instance=$1 [L,QSA]');
$_zp_conf_vars['special_pages'][] = array(
        'define' => false,
        'rewrite' => '^%FAVORITES%/*$',
        'rule' => '%REWRITE% index.php?p=favorites [L,QSA]');
if (OFFSET_PATH) {
    zp_register_filter('edit_album_custom_data', 'favorites::showWatchers');
    zp_register_filter('edit_image_custom_data', 'favorites::showWatchers');
} else {
    zp_register_filter('load_theme_script', 'favorites::loadScript');
    zp_register_filter('checkPageValidity', 'favorites::pageCount');
    zp_register_filter('admin_toolbox_global', 'favorites::toolbox', 21);
    if (zp_loggedin()) {
        if (isset($_POST['addToFavorites'])) {
            $___Favorites = new favorites($_zp_current_admin_obj->getUser());
            if (isset($_POST['instance']) && $_POST['instance']) {
                $___Favorites->instance = trim(sanitize($_POST['instance']));
                unset($_POST['instance']);
            }
            $id = sanitize($_POST['id']);
            switch ($_POST['type']) {
                case 'images':
                    $img = newImage(NULL, array('folder' => dirname($id), 'filename' => basename($id)));
                    if ($_POST['addToFavorites']) {
                        if ($img->loaded) {
                            $___Favorites->addImage($img);
                        }
                    } else {
                        $___Favorites->removeImage($img);
                    }
                    break;
                case 'albums':
                    $alb = newAlbum($id);
                    if ($_POST['addToFavorites']) {
                        if ($alb->loaded) {
                            $___Favorites->addAlbum($alb);
                        }
                    } else {
                        $___Favorites->removeAlbum($alb);
                    }
                    break;
            }
            unset($___Favorites);
            if (isset($_instance)) {
                unset($_instance);
            }
        }
        $_myFavorites = new favorites($_zp_current_admin_obj->getUser());
        function printAddToFavorites($obj, $add = NULL, $remove = NULL) {
            global $_myFavorites, $_zp_current_admin_obj, $_zp_gallery_page, $_myFavorites_button_count;
            if (!zp_loggedin() || $_myFavorites->getOwner() != $_zp_current_admin_obj->getUser() || !is_object($obj) || !$obj->exists) {
                return;
            }
            $v = 1;
            if (is_null($add)) {
                $add = get_language_string(getOption('favorites_add_button'));
            }
            if (is_null($remove)) {
                $remove = get_language_string(getOption('favorites_remove_button'));
            } else {
                $add = $remove;
            }
            $table = $obj->table;
            $target = array('type' => $table);
            if ($_zp_gallery_page == 'favorites.php') {
                
                $multi = false;
                $list = array($_myFavorites->instance);
            } else {
                if ($multi = getOption('favorites_multi')) {
                    $list = $_myFavorites->list;
                } else {
                    $list = array('');
                }
                if (extensionEnabled('tag_suggest') && !$_myFavorites_button_count) {
                    $_myFavorites_button_count++;
                    $favList = array_slice($list, 1);
                    ?>
                    <script type="text/javascript">
                        
                        var _favList = ['<?php echo implode("','", $favList); ?>'];
                        $(function() {
                            $('.favorite_instance').tagSuggest({tags: _favList})
                        });
                        
                    </script>
                    <?php
                }
            }
            $seen = array_flip($list);
            switch ($table) {
                case 'images':
                    $id = $obj->imagefolder . '/' . $obj->filename;
                    foreach ($list as $instance) {
                        $_myFavorites->instance = $instance;
                        $images = $_myFavorites->getImages(0);
                        $seen[$instance] = false;
                        foreach ($images as $image) {
                            if ($image['folder'] == $obj->imagefolder && $image['filename'] == $obj->filename) {
                                $seen[$instance] = true;
                                favorites::ad_removeButton($obj, $id, 0, $remove, $instance, $multi);
                                break;
                            }
                        }
                    }
                    if ($multi || in_array(false, $seen))
                        favorites::ad_removeButton($obj, $id, 1, $add, NULL, $multi);
                    break;
                case 'albums':
                    $id = $obj->name;
                    foreach ($list as $instance) {
                        $_myFavorites->instance = $instance;
                        $albums = $_myFavorites->getAlbums(0);
                        $seen[$instance] = false;
                        foreach ($albums as $album) {
                            if ($album == $id) {
                                $seen[$instance] = true;
                                favorites::ad_removeButton($obj, $id, 0, $remove, $instance, $multi);
                                break;
                            }
                        }
                    }
                    if ($multi || in_array(false, $seen))
                        favorites::ad_removeButton($obj, $id, 1, $add, NULL, $multi);
                    break;
                default:
                    return;
            }
        }
        function getFavoritesURL() {
            global $_myFavorites;
            return $_myFavorites->getLink();
        }
        
        function printFavoritesURL($text = NULL, $before = NULL, $between = NULL, $after = NULL) {
            global $_myFavorites;
            if (zp_loggedin()) {
                if (is_null($text)) {
                    $text = get_language_string(getOption('favorites_linktext'));
                }
                $list = $_myFavorites->getList();
                $betwixt = NULL;
                echo $before;
                foreach ($_myFavorites->getList()as $instance) {
                    $link = $_myFavorites->getLink(NULL, $instance);
                    $display = $text;
                    if ($instance) {
                        $display .= '[' . $instance . ']';
                    }
                    echo $betwixt;
                    $betwixt = $between;
                    ?>
                    <a href="<?php echo $link; ?>" class="favorite_link"><?php echo html_encode($display); ?> </a>
                    <?php
                }
                echo $after;
            }
        }
    }
}
?>