def __init__(self, obj): locale = prism.settings.PRISM_CONFIG['locale'] if isinstance(obj, BasePlugin): self.plugin_id = obj.plugin_id self.path = os.path.join(obj.plugin_folder, 'locale', locale) else: self.plugin_id = 'prism' self.path = os.path.join(obj, 'locale', locale) if not os.path.exists(self.path) and locale != 'en_US': locale = 'en_US' if isinstance(obj, BasePlugin): self.path = os.path.join(obj.plugin_folder, 'locale', 'en_US') else: self.path = os.path.join(obj, 'locale', 'en_US') prism.output( 'Locale Warning: No locale for %s. Offender: %s falling back to en_US.' % (locale, self.plugin_id)) if not os.path.exists(self.path): prism.output( 'Locale Error: Failed to load locale. %s does not exist in %s. Offender: %s' % (locale, self.path, self.plugin_id)) return with open(self.path) as f: for line in f: if '=' in line: name, value = line.split('=', 1) self.__dict__[name] = value.rstrip('\r\n')
def start(self, *args): prism.output('----------=Prism=----------') prism.settings.init(os.getpid()) prism.poof('Starting Prism') self.init_flask() self.jinja_options() self.init_flask_plugins() self.init_prism() self.start_http() return 0
def __init__(self, obj=None, filename=None, path=None, auto_save=True): if obj is not None: if filename is None: filename = 'config.json' if isinstance(obj, BasePlugin): self.path = os.path.join(obj.data_folder, filename) else: self.path = os.path.join(obj, filename) elif path is not None: self.path = path else: prism.output( 'Error: Attmpted to create a config file with no path.') self.auto_save = auto_save if not os.path.exists(self.path): self.__dict__.update({}) else: self.__dict__.update(json.loads(open(self.path).read()))
def start_http(self): prism.output('Verifying SSL') has_ssl = False try: from OpenSSL import SSL has_ssl = True except ImportError: pass if has_ssl: ssl_crt = os.path.join(prism.settings.CONFIG_FOLDER, 'prism-ssl.crt') ssl_key = os.path.join(prism.settings.CONFIG_FOLDER, 'prism-ssl.key') ssl_files_exist = os.path.exists(ssl_crt) if ssl_files_exist: ssl_files_exist = os.path.exists(ssl_key) # Generate certificate if not ssl_files_exist: prism.poof() prism.poof('Generating SSL certificate') prism.settings.generate_certificate() prism.paaf() prism.paaf() # Finally, start Prism under a self-signed SSL connection self.flask_app.run(host='0.0.0.0', port=9000, debug=True, ssl_context=(ssl_crt, ssl_key)) else: prism.output( 'Warning: Prism is starting under an insecure connection!') self.flask_app.run(host='0.0.0.0', port=9000, debug=True)
def locale_(plugin_id, s): # Search the plugin that's rendering the template for the requested locale if plugin_id == 'prism': ns = prism.settings.PRISM_LOCALE[s] else: plugin = prism.get_plugin(plugin_id) if plugin is None: prism.output('Unknown plugin ID. Offender: %s' % plugin_id) return s ns = plugin.locale[s] if s == ns: return s ns = publish_string( ns, writer=html_fragment_writer).decode('utf-8').rstrip('\r\n') if '<p>' not in ns: return '' ns = ns.split('<p>', 1)[1] ns = ns[:ns.rfind('</p>')] return jinja2.Markup(ns)
def ping_version(output=False): global PRISM_VERSIONING should_check = True if 'last_check' in PRISM_VERSIONING: should_check = ((datetime.now() - datetime.strptime( PRISM_VERSIONING['last_check'], "%Y-%m-%d %H:%M:%S")).seconds >= 60 * 60 * 2) if output or should_check: prism.poof('Collecting version info...') if should_check: rate_limited, dev_changes, recent_releases, num_releases = get_new_versions( prism.__version__) if rate_limited: prism.output('Rate limited. Version info not checked.') # If no values made yet, apply some defaults if 'dev_changes' not in PRISM_VERSIONING: PRISM_VERSIONING['dev_changes'] = [] if 'recent_releases' not in PRISM_VERSIONING: PRISM_VERSIONING['recent_releases'] = [] if 'num_releases' not in PRISM_VERSIONING: PRISM_VERSIONING['num_releases'] = 0 else: # Reset and reapply cached versioning info PRISM_VERSIONING['dev_changes'] = [] PRISM_VERSIONING['recent_releases'] = [] PRISM_VERSIONING['num_releases'] = 0 if 'dev_changes' is not None: PRISM_VERSIONING['dev_changes'] = dev_changes if 'recent_releases' is not None: PRISM_VERSIONING['recent_releases'] = recent_releases if 'num_releases' is not None: PRISM_VERSIONING['num_releases'] = num_releases if 'dev_changes' is not None or 'recent_releases' is not None or 'num_releases' is not None: PRISM_VERSIONING['last_check'] = datetime.now().strftime( "%Y-%m-%d %H:%M:%S") if output or should_check: if len(PRISM_VERSIONING['dev_changes']) > 0: prism.output('%s development commit(s) since the latest version.' % len(PRISM_VERSIONING['dev_changes'])) if len(PRISM_VERSIONING['recent_releases']) != 0: prism.poof('Current version: %s' % prism.__version__) if prism.__version__ != PRISM_VERSIONING['recent_releases'][0][ 'name']: prism.output( 'Your version is out of date. Latest version is %s' % PRISM_VERSIONING['recent_releases'][0]['name']) prism.paaf() prism.paaf()
def init(pid): global PANEL_PID, PRISM_PATH, TMP_PATH, PRISM_VERSIONING, CORE_PLUGINS_PATH, PLUGINS_PATH, \ CONFIG_FOLDER_PLUGINS, CONFIG_FOLDER, CONFIG_FILE, PRISM_CONFIG, PRISM_LOCALE PANEL_PID = pid PRISM_PATH = os.path.dirname(os.path.realpath(__file__)) CORE_PLUGINS_PATH = os.path.join(PRISM_PATH, 'core') if not os.path.exists(CORE_PLUGINS_PATH): os.makedirs(CORE_PLUGINS_PATH) PLUGINS_PATH = os.path.join(PRISM_PATH, 'plugins') if not os.path.exists(PLUGINS_PATH): os.makedirs(PLUGINS_PATH) TMP_PATH = os.path.join(PRISM_PATH, 'tmp') if not os.path.exists(TMP_PATH): os.makedirs(TMP_PATH) prism.output('Currently running in %s' % PRISM_PATH) prism.output('') PRISM_VERSIONING = JSONConfig( path=os.path.join(TMP_PATH, 'VERSIONING-INFO')) ping_version(True) prism.output('') # Load Prism's config CONFIG_FOLDER = os.path.join(PRISM_PATH, 'config') if not os.path.exists(CONFIG_FOLDER): os.makedirs(CONFIG_FOLDER) CONFIG_FILE = os.path.join(CONFIG_FOLDER, 'config.json') CONFIG_FOLDER_PLUGINS = os.path.join(CONFIG_FOLDER, 'plugins') if not os.path.exists(CONFIG_FOLDER_PLUGINS): os.makedirs(CONFIG_FOLDER_PLUGINS) PRISM_LOCALE = LocaleConfig(PRISM_PATH) # Generate default config values if the file doesn't exist # Also, prompt and generate a few of the config values that # must be done on first run. if not os.path.exists(CONFIG_FILE): # I have no idea what came over me when making this section, # but it's fabulous and I loved every second of it. I hope # I never have to change it. xD prism.output(PRISM_LOCALE['start.hello.1']) prism.output(PRISM_LOCALE['start.hello.2']) subst = {} # IP Address/Hostname prompt subst['host'] = socket.gethostbyname(socket.gethostname()) prism.output(PRISM_LOCALE['start.host'].format(**subst)) PRISM_CONFIG['host'], used_default = prism.get_input( PRISM_LOCALE['start.host.prompt'], default=subst['host']) if used_default: prism.output('') prism.output(PRISM_LOCALE['start.host.correct']) # Secret generation prism.output('') prism.output(PRISM_LOCALE['start.secret']) subst['secret_key'], used_default = prism.get_input( PRISM_LOCALE['start.secret.prompt']) prism.output('') if used_default: secret_key = prism.generate_random_string(32) prism.output(PRISM_LOCALE['start.secret.generate'].format(**subst)) else: prism.output(PRISM_LOCALE['start.secret.done'].format(**subst)) PRISM_CONFIG['secret_key'] = secret_key # Username and Password prompt prism.output('') prism.output(PRISM_LOCALE['start.login.username']) PRISM_CONFIG['username'], used_default = prism.get_input( PRISM_LOCALE['start.login.username.prompt'], default='admin') PRISM_CONFIG['password'], used_default = prism.get_input( PRISM_LOCALE['start.login.password.prompt'], default='password') if used_default: prism.output(PRISM_LOCALE['start.login.password.default.1']) time.sleep(2) prism.output(PRISM_LOCALE['start.login.password.default.2']) time.sleep(5) prism.output(PRISM_LOCALE['start.login.password.default.3']) prism.output('') prism.output('') prism.output(PRISM_LOCALE['start.done']) conf = JSONConfig(path=CONFIG_FILE) for key, value in PRISM_CONFIG.items(): conf[key] = value PRISM_CONFIG = conf else: # Load prism's config PRISM_CONFIG = JSONConfig(path=CONFIG_FILE) if 'locale' not in PRISM_CONFIG: PRISM_CONFIG['locale'] = 'en_US' if 'enabled_plugins' not in PRISM_CONFIG: PRISM_CONFIG['enabled_plugins'] = [F] # Make sure some VERY imporant values are set if 'secret_key' not in PRISM_CONFIG: prism.output(PRISM_LOCALE['start.missing.secret']) PRISM_CONFIG['secret_key'] = prism.generate_random_string(32) prism.output('') if 'host' not in PRISM_CONFIG: host = socket.gethostbyname(socket.gethostname()) prism.output(PRISM_LOCALE['start.missing.host']) PRISM_CONFIG['host'], used_default = prism.get_input( PRISM_LOCALE['start.host.prompt'], default=host) prism.output('') if 'username' not in PRISM_CONFIG: prism.output(PRISM_LOCALE['start.missing.username']) PRISM_CONFIG['username'], used_default = prism.get_input( PRISM_LOCALE['start.login.username.prompt'], default='admin') prism.output('') if 'password' not in PRISM_CONFIG: prism.output(PRISM_LOCALE['start.missing.password']) PRISM_CONFIG['password'], used_default = prism.get_input( PRISM_LOCALE['start.login.password.prompt'], default='password') prism.output('') # Detect if the password isn't md5'd. If not, hash it. This allows # the user to reset their password at any time within the config. if not prism.is_crypted(PRISM_CONFIG['password']): PRISM_CONFIG['password'] = prism.crypt_string(PRISM_CONFIG['password'])
def init_flask(self): prism.output('Initializing Flask') self.flask_app = Flask(__name__, template_folder='templates') self.flask_app.secret_key = prism.settings.PRISM_CONFIG['secret_key']