예제 #1
0
def wp_cookie_constants(self):
    ''' Defines cookie related WordPress constants
  Defines constants after multisite is loaded.
  '''
    # Used to guarantee unique hash cookies
    if not self.defined('COOKIEHASH'):
        siteurl = WiO.get_site_option('siteurl')
        if siteurl:
            self.define('COOKIEHASH', Php.md5(siteurl))
        else:
            self.define('COOKIEHASH', Php.md5(wp_guess_url()))

    if not self.defined('USER_COOKIE'):
        self.define('USER_COOKIE', 'wordpressuser_' + self.COOKIEHASH)

    if not self.defined('PASS_COOKIE'):
        self.define('PASS_COOKIE', 'wordpresspass_' + self.COOKIEHASH)

    if not self.defined('AUTH_COOKIE'):
        self.define('AUTH_COOKIE', 'wordpress_' + self.COOKIEHASH)

    if not self.defined('SECURE_AUTH_COOKIE'):
        self.define('SECURE_AUTH_COOKIE', 'wordpress_sec_' + self.COOKIEHASH)

    if not self.defined('LOGGED_IN_COOKIE'):
        self.define('LOGGED_IN_COOKIE',
                    'wordpress_logged_in_' + self.COOKIEHASH)

    if not self.defined('TEST_COOKIE'):
        self.define('TEST_COOKIE', 'wordpress_test_cookie')

    if not self.defined('COOKIEPATH'):
        self.define(
            'COOKIEPATH',
            Php.preg_replace('|https?://[^/]+|i', '',
                             WiO.get_option('home') + '/'))

    if not self.defined('SITECOOKIEPATH'):
        self.define(
            'SITECOOKIEPATH',
            Php.preg_replace('|https?://[^/]+|i', '',
                             WiO.get_option('siteurl') + '/'))

    if not self.defined('ADMIN_COOKIE_PATH'):
        self.define('ADMIN_COOKIE_PATH', self.SITECOOKIEPATH + 'wp-admin')

    if not self.defined('PLUGINS_COOKIE_PATH'):
        self.define(
            'PLUGINS_COOKIE_PATH',
            Php.preg_replace('|https?://[^/]+|i', '', self.WP_PLUGIN_URL))

    if not self.defined('COOKIE_DOMAIN'):
        self.define('COOKIE_DOMAIN', False)
예제 #2
0
파일: plugin.py 프로젝트: wordpy/wordpy
def plugin_basename(File):
    ''' Gets the basename of a plugin.
  This method extracts the name of a plugin from its filename.
  @global array wp_plugin_paths
  @param string File The filename of plugin.
  @return string The name of a plugin.
  '''
    #global var==>WpC.WB.Wj.var, except: var=WpC.WB.Wj.var=same Obj,mutable array
    wp_plugin_paths = WpC.WB.Wj.wp_plugin_paths  # global wp_plugin_paths

    # wp_plugin_paths contains normalized paths.
    File = WiFc.wp_normalize_path(File)

    Php.arsort(wp_plugin_paths)
    for Dir, realdir in wp_plugin_paths.items():
        if Php.strpos(File, realdir) == 0:  # ===
            File = Dir + Php.substr(File, Php.strlen(realdir))

    plugin_dir = WiFc.wp_normalize_path(WpC.WB.Wj.WP_PLUGIN_DIR)
    mu_plugin_dir = WiFc.wp_normalize_path(WpC.WB.Wj.WPMU_PLUGIN_DIR)

    File = Php.preg_replace(
        '#^' + Php.preg_quote(plugin_dir, '#') + '/|^' +
        Php.preg_quote(mu_plugin_dir, '#') + '/#', '', File)
    # get relative path from plugins dir
    File = Php.trim(File, '/')
    return File
예제 #3
0
def wp_normalize_path(path):
    ''' Normalize a filesystem path.
  On windows systems, replaces backslashes with forward slashes
  and forces upper-case drive letters.
  Allows for two leading slashes for Windows network shares, but
  ensures that all other duplicate slashes are reduced to a single.
  @since 4.4.0 Ensures upper-case drive letters on Windows systems.
  @since 4.5.0 Allows for Windows network shares.
  @param string path Path to normalize.
  @return string Normalized path.
  '''
    path = Php.str_replace('\\', '/', path)
    path = Php.preg_replace('|(?<=.)/+|', '/', path)
    if ':' == Php.substr(path, 1, 1):
        path = Php.ucfirst(path)
    return path
예제 #4
0
def sanitize_title_with_dashes(title, raw_title='', context='display'):
    ''' Sanitizes a title, replacing whitespace and a few other characters with dashes.
 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
 * Whitespace becomes a dash.
 * @param string title     The title to be sanitized.
 * @param string raw_title Optional. Not used.
 * @param string context   Optional. The operation for which the string is sanitized.
 * @return string The sanitized title.
  '''
    title = Php.strip_tags(title)
    # Preserve escaped octets.
    #title= Php.preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', title)
    title = Php.preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---\\1---', title)
    # Remove percent signs that are not part of an octet.
    title = Php.str_replace('%', '', title)
    # Restore octets.
    #title= Php.preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', title)
    title = Php.preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%\\1', title)

    #if seems_utf8(title):
    if Php.function_exists('mb_strtolower'):
        title = Php.mb_strtolower(title, 'UTF-8')
    title = utf8_uri_encode(title, 200)

    title = Php.strtolower(title)

    if 'save' == context:
        # Convert nbsp, ndash and mdash to hyphens
        title = Php.str_replace(('%c2%a0', '%e2%80%93', '%e2%80%94'), '-',
                                title)
        # Convert nbsp, ndash and mdash HTML entities to hyphens
        title = Php.str_replace(
            ('&nbsp;', '&#160;', '&ndash;', '&#8211;', '&mdash;', '&#8212;'),
            '-', title)

        # Strip these characters entirely
        title = Php.str_replace(
            (
                # iexcl and iquest
                '%c2%a1',
                '%c2%bf',
                # angle quotes
                '%c2%ab',
                '%c2%bb',
                '%e2%80%b9',
                '%e2%80%ba',
                # curly quotes
                '%e2%80%98',
                '%e2%80%99',
                '%e2%80%9c',
                '%e2%80%9d',
                '%e2%80%9a',
                '%e2%80%9b',
                '%e2%80%9e',
                '%e2%80%9f',
                # copy, reg, deg, hellip and trade
                '%c2%a9',
                '%c2%ae',
                '%c2%b0',
                '%e2%80%a6',
                '%e2%84%a2',
                # acute accents
                '%c2%b4',
                '%cb%8a',
                '%cc%81',
                '%cd%81',
                # grave accent, macron, caron
                '%cc%80',
                '%cc%84',
                '%cc%8c',
            ),
            '',
            title)

        # Convert times to x
        title = Php.str_replace('%c3%97', 'x', title)

    title = Php.preg_replace('/&.+?;/', '', title)
    # kill entities
    title = Php.str_replace('.', '-', title)

    title = Php.preg_replace('/[^%a-z0-9 _-]/', '', title)
    title = Php.preg_replace('/\s+/', '-', title)
    title = Php.preg_replace('|-+|', '-', title)
    title = Php.trim(title, '-')

    return title
예제 #5
0
파일: meta.py 프로젝트: wordpy/wordpy
def update_meta_cache(meta_type, object_ids):
    '''Update the metadata cache for the specified objects.
  @param string    meta_type  Type of object metadata is for
                              (e.g., comment, post, or user)
  @param int|array object_ids Array or comma delimited list of object IDs
                              to update cache for
  @return array|False         Metadata cache for the specified objects,
                              or False on failure.
  '''
    #global var==>WpC.WB.Wj.var, except: var=WpC.WB.Wj.var=same Obj,mutable array
    wpdb = WpC.WB.Wj.wpdb  # global wpdb

    print('update_meta_cache meta_type={}, object_ids={}'.format(
        meta_type, object_ids))

    if not meta_type or not object_ids:
        print('update_meta_cache not meta_type or not object_ids:')
        return False
    table = _get_meta_table(meta_type)
    if not table:
        print('update_meta_cache not table:')
        return False

    column = WiF.sanitize_key(meta_type + '_id')

    #if not isinstance(object_ids, (list,tuple,)):
    if not Php.is_array(object_ids):
        #import re
        #Re = re.compile('[^0-9,]')
        #object_ids = Re.sub('', object_ids)
        object_ids = Php.preg_replace('|[^0-9,]|', '', object_ids)
        #object_ids= object_ids.split(',')
        object_ids = Php.explode(',', object_ids)
        # object_ids = [ val for val in object_ids.values() ]

    #object_ids = [int(val) for val in object_ids]   #same as:
    object_ids = Php.array_map(Php.intval, object_ids)
    print('update_meta_cache object_ids=', object_ids)

    cache_key = meta_type + '_meta'
    ids = array()
    cache = array()
    for Id in object_ids:
        cached_object = WiCa.wp_cache_get(Id, cache_key)
        if False is cached_object:
            ids[None] = Id
        else:
            cache[Id] = cached_object

    if not ids:  # if Php.empty(locals(), 'ids'):
        return cache

    # Get meta info
    #id_list = ','.join(str(ids))  #BAD!!#= [,2,6,8,3,4,]
    id_list = ','.join([str(Id) for Id in ids])
    id_column = 'umeta_id' if 'user' == meta_type else 'meta_id'
    #meta_list = wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A )
    # in wp-db.php get_results( , ARRAY_A) = column name-keyed row arrays
    Sql = ("SELECT {}, meta_key, meta_value FROM {} WHERE {} IN ({}) ORDER BY "
           "{} ASC".format(column, table, column, id_list, id_column))
    #meta_list = wDB.GetDB(MetaC, table).Exec(Sql)
    meta_list = wpdb.get_results(Sql, 'ARRAY_A')
    # in wp-db.php get_results( , ARRAY_A) = column name-keyed row arrays
    print('\n update_meta_cache meta_list =', meta_list)

    if meta_list:  # if not Php.empty(locals(), 'meta_list'):
        for metarow in meta_list:
            mpid = int(metarow[column])
            mkey = metarow['meta_key']
            mval = metarow['meta_value']

            # Force subkeys to be arry type:
            if not Php.isset(cache, mpid) or not Php.is_array(cache[mpid]):
                cache[mpid] = array()
            if (not Php.isset(cache[mpid], mkey)
                    or not Php.is_array(cache[mpid][mkey])):
                cache[mpid][mkey] = array()

            # Add a value to the current pid/key:
            cache[mpid][mkey][None] = mval

    for Id in ids:
        if not Php.isset(cache, Id):
            cache[Id] = array()
        WiCa.wp_cache_add(Id, cache[Id], cache_key)

    print('\n update_meta_cache return cache =', cache)
    return cache