/usr/share/cherokee/admin/Icons.py is in cherokee-admin 1.2.101-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
#
# Cherokee-admin
#
# Authors:
# Alvaro Lopez Ortega <alvaro@alobbs.com>
# Taher Shihadeh <taher@octality.com>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import CTK
import Page
import Cherokee
import os
import validations
import util
from consts import *
from configured import *
URL_APPLY = '/icons/apply'
ICON_ROW_SIZE = 9
VALIDATIONS = [
('new_exts', validations.is_safe_icons_suffix),
('new_file', validations.is_safe_icons_file),
]
def modify():
updates = {}
for k in CTK.post:
# Presses 'submit' without changing anything
if k in ('icons!default', 'icons!directory', 'icons!parent_directory'):
if not CTK.post.get_val(k):
return {'ret': "error"}
# Validations for files and suffixes
validator = None
if k.startswith('icons!suffix') and CTK.post[k]:
validator = validations.is_safe_icons_suffix
elif k.startswith ('icons!file') and CTK.post[k]:
validator = validations.is_safe_icons_file
if validator:
new = CTK.post[k]
try:
val = validator (new, CTK.cfg.get_val(k))
if util.lists_differ (val, new):
updates[k] = val
except ValueError, e:
return {'ret': "error", 'errors': {k: str(e)}}
if updates:
return {'ret': 'unsatisfactory', 'updates': updates}
def commit():
# New extension
new_exts = CTK.post.pop('new_exts')
if new_exts:
icon = CTK.post.get_val('new_exts_icon')
if not icon:
return CTK.cfg_reply_ajax_ok()
CTK.cfg['icons!suffix!%s'%(icon)] = new_exts
return CTK.cfg_reply_ajax_ok()
# New file
new_file = CTK.post.pop('new_file')
if new_file:
icon = CTK.post.get_val('new_file_icon')
if not icon:
return CTK.cfg_reply_ajax_ok()
CTK.cfg['icons!file!%s'%(icon)] = new_file
return CTK.cfg_reply_ajax_ok()
# Modifications
updates = modify()
if updates:
return updates
return CTK.cfg_apply_post()
def prettyfier (filen):
# Remove extension
if '.' in filen:
filen = filen[:filen.rindex('.')]
# Remove underscores
filen = filen.replace('_',' ').replace('.',' ')
# Capitalize
return filen.capitalize()
class ExtensionsTable (CTK.Container):
def __init__ (self, refreshable, **kwargs):
CTK.Container.__init__ (self, **kwargs)
self += CTK.RawHTML ("<h2>%s</h2>" %_('Extension List'))
# List
icons = CTK.cfg.keys('icons!suffix')
if not icons:
button_class = 'no_extensions'
else:
button_class = 'extensions'
table = CTK.Table()
table.id = "icon_extensions"
table += [None, CTK.RawHTML(_('Extensions'))]
table.set_header(1)
icons.sort()
for k in icons:
pre = 'icons!suffix!%s'%(k)
desc = prettyfier(k)
image = CTK.Image ({'alt' : desc, 'title': desc, 'src': os.path.join('/icons_local', k)})
delete = CTK.ImageStock('del')
submit = CTK.Submitter (URL_APPLY)
submit += CTK.TextCfg (pre, props={'size': '46'})
table += [image, submit, delete]
delete.bind('click', CTK.JS.Ajax (URL_APPLY, data = {pre: ''},
complete = refreshable.JS_to_refresh()))
self += CTK.Indenter (table)
# Add New
button = AdditionDialogButton ('new_exts', _('Add New Extension'), klass = button_class)
button.bind ('submit_success', refreshable.JS_to_refresh())
self += button
class FilesTable (CTK.Container):
def __init__ (self, refreshable, **kwargs):
CTK.Container.__init__ (self, **kwargs)
self += CTK.RawHTML ("<h2>%s</h2>" %_('File Matches'))
# List
icons = CTK.cfg.keys('icons!file')
if not icons:
button_class = 'no_file'
else:
button_class = 'file'
table = CTK.Table()
table.id = "icon_files"
table += [None, CTK.RawHTML(_('Files'))]
table.set_header(1)
for k in icons:
pre = 'icons!file!%s'%(k)
desc = prettyfier(k)
image = CTK.Image ({'alt' : desc, 'title': desc, 'src': os.path.join('/icons_local', k)})
submit = CTK.Submitter (URL_APPLY)
submit += CTK.TextCfg (pre, props={'size': '46'})
delete = CTK.ImageStock('del')
table += [image, submit, delete]
delete.bind('click', CTK.JS.Ajax (URL_APPLY, data = {pre: ''},
complete = refreshable.JS_to_refresh()))
self += CTK.Indenter (table)
# Add New
button = AdditionDialogButton ('new_file', _('Add New File'), klass = button_class)
button.bind ('submit_success', refreshable.JS_to_refresh())
self += button
class SpecialTable (CTK.Container):
def __init__ (self, refreshable, **kwargs):
CTK.Container.__init__ (self, **kwargs)
table = CTK.Table()
for k, desc in [('default', _('Default')),
('directory', _('Directory')),
('parent_directory', _('Go to Parent'))]:
icon = CTK.cfg.get_val('icons!%s'%(k), 'blank.png')
props = {'alt' : desc,
'title': desc,
'src' : '/icons_local/%s'%(icon)}
image = CTK.Image (props)
modify = _('Modify')
button = AdditionDialogButton (k, "%(modify)s '%(desc)s'"%(locals()), selected = icon, submit_label = modify)
button.bind ('submit_success', refreshable.JS_to_refresh())
table += [image, CTK.RawHTML(desc), button]
submit = CTK.Submitter (URL_APPLY)
submit += table
self += CTK.RawHTML ("<h2>%s</h2>" %_('Special Files'))
self += CTK.Indenter (submit)
class SpecialWidget_Instancer (CTK.Container):
def __init__ (self):
CTK.Container.__init__ (self)
# Refresher
refresh = CTK.Refreshable ({'id': 'icons_special'})
refresh.register (lambda: SpecialTable(refresh).Render())
self += refresh
class ExtensionsWidget_Instancer (CTK.Container):
def __init__ (self):
CTK.Container.__init__ (self)
# Refresher
refresh = CTK.Refreshable ({'id': 'icons_extensions'})
refresh.register (lambda: ExtensionsTable(refresh).Render())
self += refresh
class FilesWidget_Instancer (CTK.Container):
def __init__ (self):
CTK.Container.__init__ (self)
# Refresher
refresh = CTK.Refreshable ({'id': 'icons_files'})
refresh.register (lambda: FilesTable(refresh).Render())
self += refresh
ICON_CHOOSER_JS = """
$('#%(iconchooserbox_id)s').each(function() {
var chooser = $(this);
var hidden = chooser.find('input:hidden');
chooser.find('img.icon_chooser_entry').each(function() {
$(this).bind('click', function(event) {
var image = $(this);
hidden.val(image.attr('file'));
chooser.find('.icon_chooser_entry').removeClass("icon_chooser_selected");
image.addClass("icon_chooser_selected");
});
});
});
"""
class IconChooser (CTK.Box):
def __init__ (self, field_name, prefix, **kwargs):
CTK.Box.__init__ (self, {'class': 'icon-chooser-box'})
selected = kwargs.get('selected')
# Get the file list
files, unused = self.get_files (prefix)
total = len(files)
# Build the table
row = []
table = CTK.Table()
for x in range(total):
filename = files[x]
desc = prettyfier (filename)
props = {'alt' : desc,
'title': desc,
'file' : filename,
'src' : '/icons_local/%s'%(filename),
'class': 'icon_chooser_entry icon_chooser_used'}
if filename in unused:
props['class'] = 'icon_chooser_entry icon_chooser_unused'
if filename == selected:
props['class'] = 'icon_chooser_entry icon_chooser_selected'
image = CTK.Image(props)
row.append (image)
if (x+1) % ICON_ROW_SIZE == 0 or (x+1) == total:
table += row
row = []
hidden = CTK.Hidden(field_name, '')
self += hidden
self += table
def Render (self):
render = CTK.Box.Render (self)
render.js += ICON_CHOOSER_JS %({'iconchooserbox_id': self.id})
return render
def get_files (self, prefix = None):
listdir = os.listdir (CHEROKEE_ICONSDIR)
files = []
for filename in listdir:
ext = filename.lower().split('.')[-1]
if ext in ('jpg', 'png', 'gif', 'svg'):
files.append(filename)
if not prefix:
return (files, files)
icons = CTK.cfg.keys(prefix)
unused = list(set(files) - set(icons))
unused.sort()
return (files, unused)
class AddIcon (CTK.Container):
def __init__ (self, key, **kwargs):
CTK.Container.__init__ (self)
descs = {'new_file': _('File'),
'new_exts': _('Extensions'),
'default': _('Default'),
'directory': _('Directory'),
'parent_directory': _('Go to Parent')}
assert key in descs.keys()
label = descs[key]
table = CTK.Table()
row = [CTK.Box ({'class': 'icon_label'}, CTK.RawHTML (label))]
if key in ['new_file','new_exts']:
field = CTK.TextField({'name': key, 'class': 'noauto'})
row.append(CTK.Box ({'class': 'icon_field'}, field))
hidden_name = '%s_icon' %(key)
prefix = ['icons!suffix', 'icons!file'][key == 'new_file']
else:
hidden_name = 'icons!%s' %(key)
prefix = None
table += row
submit = CTK.Submitter (URL_APPLY)
submit += table
submit += IconChooser(hidden_name, prefix, **kwargs)
self += submit
class AdditionDialogButton (CTK.Box):
def __init__ (self, key, title, **kwargs):
CTK.Box.__init__ (self, {'class': '%s-button' %(kwargs.get('klass', 'icon'))})
submit_label = kwargs.get('submit_label', _('Add'))
button_label = kwargs.get('button_label', submit_label)
# Dialog
dialog = CTK.Dialog ({'title': title, 'width': 375})
dialog.AddButton (_('Cancel'), "close")
dialog.AddButton (submit_label, dialog.JS_to_trigger('submit'))
dialog += AddIcon(key, **kwargs)
# Button
button = CTK.Button (button_label)
button.bind ('click', dialog.JS_to_show())
dialog.bind ('submit_success', dialog.JS_to_close())
dialog.bind ('submit_success', self.JS_to_trigger('submit_success'));
self += button
self += dialog
class Icons_Widget (CTK.Box):
def __init__ (self, **kwargs):
CTK.Box.__init__ (self, {'class':'icons'}, **kwargs)
self += SpecialWidget_Instancer()
self += FilesWidget_Instancer()
self += ExtensionsWidget_Instancer()
CTK.publish ('^%s'%(URL_APPLY), commit, validation=VALIDATIONS, method="POST")
|