Пример #1
0
 function _produce_borrowing_nav()
 {
     $ret = '';
     $nes = new entity_selector();
     $nes->add_type(id_of('type'));
     $nes->add_right_relationship($this->admin_page->site_id, relationship_id_of('site_cannot_edit_type'));
     $nes->add_relation('`entity`.`id` = "' . reason_sql_string_escape($this->admin_page->type_id) . '"');
     $nes->set_num(1);
     $nes->limit_tables();
     $nes->limit_fields();
     $ns = $nes->run_one();
     $show_edit = reason_user_has_privs($this->admin_page->user_id, 'edit') && !$this->admin_page->is_second_level() && empty($ns) ? true : false;
     /* $type = new entity($this->admin_page->type_id);
     			$name = $type->get_value('plural_name') ? $type->get_value('plural_name') : $type->get_value('name');
     			if(function_exists('mb_strtolower'))
     				$name = mb_strtolower($name);
     			else
     				$name = strtolower($name); */
     $ret .= '<div class="borrowNav">' . "\n";
     $ret .= '<ul>';
     if ($show_edit) {
         $ret .= '<li><a href="' . $this->admin_page->get_owned_list_link($this->admin_page->type_id) . '"><img src="' . REASON_HTTP_BASE_PATH . 'silk_icons/bullet_edit.png" alt="" /> Add &amp; edit</a></li>';
     }
     $ret .= '<li class="current"><strong><img src="' . REASON_HTTP_BASE_PATH . 'silk_icons/car.png" alt="" /> Borrow</strong></li>';
     $ret .= '</ul>' . "\n";
     $ret .= '</div>' . "\n";
     // if(reason_user_has_privs($this->admin_page->user_id,'edit'))
     return $ret;
 }
function relationship_find_and_update($a_type, $b_type, $name, $updates = array())
{
    $existing_rel_id = relationship_finder($a_type, $b_type, $name);
    if (!empty($existing_rel_id) && !empty($updates)) {
        // build criteria clause - only want to update if it is actually needed
        $set_str = $where_str_body = '';
        $where_str_start = " AND (";
        foreach ($updates as $k => $v) {
            $set_str .= !empty($set_str) ? ", " : '';
            $where_str_body .= !empty($where_str_body) ? ") OR (" : "(";
            $where_str_body .= $k . ' != "' . reason_sql_string_escape($v) . '"';
            $set_str .= $k . ' = "' . reason_sql_string_escape($v) . '"';
        }
        $where_str_end = "))";
        $q = 'UPDATE allowable_relationship SET ' . $set_str . ' WHERE ID=' . $existing_rel_id . $where_str_start . $where_str_body . $where_str_end;
        db_query($q, 'could not update the places a blog on a page relationship');
        $num_rows = mysql_affected_rows();
        if (!empty($num_rows)) {
            echo '<p>updated relationship ' . $name . '</p>';
            pray($updates);
            return true;
        } else {
            return false;
        }
    }
}
Пример #3
0
 function _get_events()
 {
     if (!isset($this->events)) {
         $es = new entity_selector($this->site_id);
         $es->add_type(id_of('event_type'));
         if (!in_array('archived', $this->params['show'])) {
             $es->add_relation('`last_occurence` >= "' . reason_sql_string_escape(date('Y-m-d')) . '"');
         }
         if (!in_array('upcoming', $this->params['show'])) {
             $es->add_relation('`datetime` < "' . reason_sql_string_escape(date('Y-m-d', time() + 60 * 60 * 24)) . '"');
         }
         if (!in_array('current', $this->params['show'])) {
             $es->add_relation('(`last_occurence` < "' . reason_sql_string_escape(date('Y-m-d')) . '" OR `datetime` >= "' . reason_sql_string_escape(date('Y-m-d', time() + 60 * 60 * 24)) . '")');
         }
         $es->add_relation('`show_hide` = "show"');
         $es->set_order($this->params['order']);
         $this->_modify_events_es($es);
         $events = $es->run_one();
         $class = $this->get_model_class($this->params['model']);
         foreach ($events as $id => $event) {
             $this->events[$id] = new $class($event);
         }
         if (empty($this->events)) {
             $this->events = array();
         }
     }
     return $this->events;
 }
Пример #4
0
function is_site_root($page_id)
{
    $query = 'SELECT * FROM relationship WHERE entity_a="' . reason_sql_string_escape($page_id) . '" AND type="' . relationship_id_of('minisite_page_parent') . '"';
    $results = db_query($query);
    while ($row = mysql_fetch_array($results)) {
        if ($row['entity_b'] == $page_id) {
            return true;
        }
    }
    return false;
}
Пример #5
0
 function run_error_checks()
 {
     parent::run_error_checks();
     $es = new entity_selector();
     $es->add_relation('audience_integration.directory_service_value = "' . reason_sql_string_escape($this->get_value('directory_service_value')) . '"');
     $es->add_relation('entity.id != ' . $this->get_value('id'));
     $es->set_num(1);
     $conflicts = $es->run_one(id_of('audience_type'));
     if (!empty($conflicts)) {
         $this->set_error('directory_service_value', 'The Directory Service Value you entered ("' . $this->get_value('directory_service_value') . '") is already in use. Each audience must have a unique directory service value.');
     }
 }
Пример #6
0
 function _get_active_user_ids($since_datetime)
 {
     if (empty($since_datetime)) {
         trigger_error('Please provide a date');
         return array();
     }
     $q = 'SELECT DISTINCT `last_edited_by` FROM `entity` WHERE `last_modified` >= "' . reason_sql_string_escape($since_datetime) . '" AND `type` NOT IN ("' . id_of('comment_type') . '","' . id_of('classified_type') . '")';
     $r = db_query($q, 'Unable to get active users');
     $ids = array();
     while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
         $ids[] = $row['last_edited_by'];
     }
     mysql_free_result($r);
     return $ids;
 }
Пример #7
0
 function init($args = array())
 {
     parent::init($args);
     $es = new entity_selector();
     $es->description = 'Selecting images for the gallery';
     $es->add_type(id_of('image'));
     $es = $this->refine_es($es);
     if (!empty($this->request['search_image'])) {
         $es->add_relation('(entity.name LIKE "%' . reason_sql_string_escape($this->request['search_image']) . '%" OR meta.description LIKE "%' . reason_sql_string_escape($this->request['search_image']) . '%"' . ' OR meta.keywords LIKE "%' . reason_sql_string_escape($this->request['search_image']) . '%"' . ' OR chunk.content LIKE "%' . reason_sql_string_escape($this->request['search_image']) . '%"' . ')');
     }
     $this->num = $es->get_one_count();
     $this->check_bounds();
     $es->set_num($this->num_per_page);
     $es->set_start(($this->request['page'] - 1) * $this->num_per_page);
     $this->images = $es->run_one();
 }
Пример #8
0
 function alter_feed()
 {
     // Start with defaults
     $this->do_default_field_mapping();
     // Then change only the link field
     $this->feed->set_item_field_map('link', 'id');
     $this->feed->set_item_field_handler('link', 'make_link', true);
     $this->feed->set_item_field_map('enclosure', 'id');
     // Modify entity selector
     $num = !empty($_REQUEST['num']) ? turn_into_int($_REQUEST['num']) : '0';
     $start = !empty($_REQUEST['start']) ? turn_into_int($_REQUEST['start']) : '0';
     if (!empty($_REQUEST['q'])) {
         $this->feed->es->add_relation('(entity.name LIKE "%' . reason_sql_string_escape($_REQUEST['q']) . '%"' . ' OR meta.description LIKE "%' . reason_sql_string_escape($_REQUEST['q']) . '%"' . ' OR meta.keywords LIKE "%' . reason_sql_string_escape($_REQUEST['q']) . '%"' . ' OR chunk.content LIKE "%' . reason_sql_string_escape($_REQUEST['q']) . '%"' . ')');
     }
     $this->feed->es->set_num($num);
     $this->feed->es->set_start($start);
     $this->feed->es->set_order('entity.last_modified DESC, dated.datetime DESC, entity.name ASC');
 }
 /**
  * Validate username and password 
  * @access public
  * @param string $username Userid
  * @param string $password Password
  * @return boolean
  */
 function authenticate($username, $password)
 {
     settype($username, 'string');
     settype($password, 'string');
     if (!empty($username) && !empty($password)) {
         $es = new entity_selector();
         $es->add_relation('entity.name = "' . reason_sql_string_escape($username) . '"');
         $es->add_relation('user.user_password_hash = "' . sha1($password) . '"');
         $es->add_relation($this->get_basic_limitation());
         $es->set_num(1);
         $this->open_conn();
         $users = $es->run_one(id_of('user'));
         $this->close_conn();
         if (!empty($users)) {
             return true;
         }
     }
     return false;
 }
Пример #10
0
 /**
  * @return an html string contain the controls for resizing the live preview
  *
  */
 function get_preview_controls()
 {
     //get the parameters, location, and page types that use feature
     //and place the height and width as option tag values, and page type
     // and location as what the user sees when using the select box.
     $rpts =& get_reason_page_types();
     $ptypes = $rpts->get_params_of_page_types_that_use_module('feature/feature');
     $types = array();
     $contents = array();
     foreach ($ptypes as $type) {
         $types[$type['page_type']] = $type;
     }
     //		pray($types);
     if (!empty($types)) {
         $prepped = array();
         foreach ($types as $pt => $type) {
             $prepped[] = reason_sql_string_escape($pt);
         }
         $es = new entity_selector();
         $es->add_type(id_of('minisite_page'));
         $es->add_relation('custom_page IN ("' . implode('","', $prepped) . '")');
         $es->add_left_relationship($this->get_value('id'), relationship_id_of('page_to_feature'));
         $placed_pages = $es->run_one();
         //			pray($placed_pages);
         foreach ($placed_pages as $page) {
             $w = $this->width;
             $h = $this->height;
             if (!empty($types[$page->get_value('custom_page')]['params']['width'])) {
                 $w = htmlspecialchars($types[$page->get_value('custom_page')]['params']['width']);
             }
             if (!empty($types[$page->get_value('custom_page')]['params']['height'])) {
                 $h = htmlspecialchars($types[$page->get_value('custom_page')]['params']['height']);
             }
             $name = strip_tags($page->get_value('name'));
             $contents[] = array('name' => $name, 'w' => $w, 'h' => $h, 'italicize' => true);
         }
         $es = new entity_selector($this->get_value('site_id'));
         $es->add_type(id_of('minisite_page'));
         $es->add_relation('custom_page IN ("' . implode('","', $prepped) . '")');
         if (!empty($placed_pages)) {
             $es->add_relation('entity.id NOT IN ("' . implode('","', array_keys($placed_pages)) . '")');
         }
         $site_pages = $es->run_one();
         foreach ($site_pages as $page) {
             $w = $this->width;
             $h = $this->height;
             if (!empty($types[$page->get_value('custom_page')]['params']['width'])) {
                 $w = htmlspecialchars($types[$page->get_value('custom_page')]['params']['width']);
             }
             if (!empty($types[$page->get_value('custom_page')]['params']['height'])) {
                 $h = htmlspecialchars($types[$page->get_value('custom_page')]['params']['height']);
             }
             $name = strip_tags($page->get_value('name'));
             $contents[] = array('name' => $name, 'w' => $w, 'h' => $h);
         }
     }
     // end if(!empty($types))
     if (empty($contents)) {
         $contents[] = array('name' => "Default Size (No pages show features on the site yet)", 'w' => $this->width, 'h' => $this->height);
     }
     //testing with a ton O' tabs
     /*
     		for($i=100;$i<400;$i=$i+10)
     		{
     			$contents[]=array('name'=>"foo$i",'w'=>$i,'h'=>$i);
     		}
     
     
     		//testing with a ton O' pages
     		for($i=100;$i<400;$i++)
     		{
     			$contents[]=array('name'=>"foo$i",'w'=>400,'h'=>300);
     		}
     */
     $tabs = new Feature_Tabs();
     $tabs->set($contents);
     $w = $this->width;
     $h = $this->height;
     //		echo $w."x".$h;
     //		$tabs->set_active_tab($w."x".$h);
     $tab_html_str = $tabs->get_html();
     $str = "<h4 class=\"size_label\">Preview At Different Sizes </h4>";
     $str .= $tab_html_str;
     return $str;
 }
Пример #11
0
 function _param_to_sql_set($param)
 {
     if (is_array($param)) {
         array_walk($param, 'db_prep_walk');
         return implode(',', $param);
     } else {
         return '"' . reason_sql_string_escape($param) . '"';
     }
 }
Пример #12
0
/**
 * Move all the fields of one table into another table for a specific type
 *
 * This method is for denormalizing Reason tables. For example, a type may use a common table
 * like meta, datetime, or chunk. For performance reasons, it can be desirable to collapse
 * these tables into a single table just for that type. This method will do that.
 * 
 * @param integer $type The ID of the type whose fields we are moving
 * @param string $source_table The name of the table we are moving fields FROM
 * @param string $destination_table The name of the table we are moving fields TO
 * @param integer $user_id The Reason ID of the user who is doing this move
 * @return boolean Success
 *
 * @todo Add limit to ensure fields are only created that don't already exist
 */
function reason_move_table_fields($type, $source_table, $destination_table, $user_id)
{
    // Sanity checks
    if (empty($type)) {
        trigger_error('No type provided in reason_move_table_fields()');
        return false;
    }
    if (empty($source_table)) {
        trigger_error('No source table provided in reason_move_table_fields()');
        return false;
    }
    if (!is_string($source_table)) {
        trigger_error('Source table provided not a string in reason_move_table_fields()');
        return false;
    }
    if (empty($destination_table)) {
        trigger_error('No destination table provided in reason_move_table_fields()');
        return false;
    }
    if (!is_string($destination_table)) {
        trigger_error('Destination table provided not a string in reason_move_table_fields()');
        return false;
    }
    if ('entity' == $source_table || 'entity' == $destination_table) {
        trigger_error('reason_move_table_fields() cannot move fields into or out of the entity table.');
        return false;
    }
    if (is_object($type)) {
        $type_id = $type->id();
    } elseif (is_numeric($type)) {
        $type_id = (int) $type;
    } else {
        $type_id = id_of($type);
    }
    if (empty($type_id)) {
        trigger_error('Invalid type specified in reason_move_table_fields().');
        return false;
    }
    if (is_object($type)) {
        $type_entity = $type;
    } else {
        $type_entity = new entity($type_id);
    }
    $type_vals = $type_entity->get_values();
    if (empty($type_vals)) {
        trigger_error('Type specified (id ' . $type_id . ') is not a Reason entity in reason_move_table_fields().');
        return false;
    }
    if ($type_entity->get_value('type') != id_of('type')) {
        trigger_error('Type specified (id ' . $type_id . ') is not a Type entity in reason_move_table_fields().');
        return false;
    }
    if ($type_entity->get_value('state') != 'Live') {
        trigger_error('Type specified (id ' . $type_id . ') is not a live entity in reason_move_table_fields().');
        return false;
    }
    if (empty($user_id)) {
        trigger_error('No user id specified in reason_move_table_fields().');
        return false;
    }
    $user = new entity($user_id);
    if (!$user->get_values() || $user->get_value('type') != id_of('user')) {
        trigger_error('Invalid user ID specified in reason_move_table_fields().');
        return false;
    }
    // check for table existence
    $es = new entity_selector();
    $es->add_type(id_of('content_table'));
    $es->add_relation('`name` = "' . reason_sql_string_escape($source_table) . '"');
    $source_table_result = $es->run_one();
    if (empty($source_table_result)) {
        trigger_error('Source table "' . $source_table . '" does not exist in reason_move_table_fields()');
        return false;
    }
    $es = new entity_selector();
    $es->add_type(id_of('content_table'));
    $es->add_relation('`name` = "' . reason_sql_string_escape($destination_table) . '"');
    $destination_table_result = $es->run_one();
    if (empty($destination_table_result)) {
        trigger_error('Destination table "' . $destination_table . '" does not exist in reason_move_table_fields()');
        return false;
    }
    $source_table_entity = current($source_table_result);
    $destination_table_entity = current($destination_table_result);
    // ensure type uses both tables
    $type_tables = get_entity_tables_by_type($type_id);
    if (!in_array($source_table, $type_tables)) {
        trigger_error('Source table "' . $source_table . '" not part of the type in reason_move_table_fields()');
        return false;
    }
    if (!in_array($destination_table, $type_tables)) {
        trigger_error('Destination table "' . $destination_table . '" not part of the type in reason_move_table_fields()');
        return false;
    }
    $es = new entity_selector();
    $es->add_type(id_of('type'));
    $es->add_left_relationship($destination_table_entity->id(), relationship_id_of('type_to_table'));
    $es->add_relation('`entity`.`id` != "' . reason_sql_string_escape($type_id) . '"');
    $other_types = $es->run_one();
    if (!empty($other_types)) {
        trigger_error(count($other_types) . ' other type(s) share the destination table with the type specified in reason_move_table_fields(). reason_move_table_fields() can only move fields into single-type tables.');
        return false;
    }
    // get the fields in the old table
    $es = new entity_selector();
    $es->add_type(id_of('field'));
    $es->add_left_relationship($source_table_entity->id(), relationship_id_of('field_to_entity_table'));
    $source_table_fields = $es->run_one();
    if (empty($source_table_fields)) {
        trigger_error('Source table ' . $source_table . ' does not appear to have any fields associated with it in Reason. Unable to move its content in reason_move_table_fields()');
    }
    $q = 'DESCRIBE `' . reason_sql_string_escape($destination_table) . '`';
    $handle = db_query($q, 'Unable to describe destination table in reason_move_table_fields()');
    $raw_dest_cols = array();
    while ($row = mysql_fetch_assoc($handle)) {
        $raw_dest_cols[] = $row['Field'];
    }
    foreach ($source_table_fields as $k => $field) {
        if (in_array($field->get_value('name'), $raw_dest_cols)) {
            trigger_error($field->get_value('name') . ' field is already in destination table. Unable to accomplish reason_move_table_fields().');
            return false;
        }
        $tmp_field_name = $field->get_value('name') . '_move_tmp';
        if (in_array($tmp_field_name, $raw_dest_cols)) {
            trigger_error($tmp_field_name . ' field already in destination table. There appears to have been an error in a previous attempt to run reason_move_table_fields(). Please drop this column in MySQL and try again.');
            return false;
        }
        $source_table_fields[$k]->set_value('_field_move_temp_name', $field->get_value('name') . '_move_tmp');
    }
    // Done with sanity checks
    // map old to temp field names & create new fields
    $query_parts = array();
    foreach ($source_table_fields as $k => $field) {
        $source_table_fields[$k]->set_value('_field_move_temp_name', $field->get_value('name') . '_move_tmp');
        $q = 'ALTER TABLE `' . reason_sql_string_escape($destination_table) . '` ADD ' . reason_sql_string_escape($field->get_value('_field_move_temp_name')) . ' ' . $field->get_value('db_type');
        db_query($q, 'Unable to create new field ' . $field->get_value('_field_move_temp_name') . ' in reason_move_table_fields()');
        $values = array();
        foreach ($field->get_values() as $f => $v) {
            if ($f != 'name' && $f != 'id' && strpos($f, '_') !== 0) {
                $values[$f] = $v;
            }
        }
        $id = reason_create_entity(id_of('master_admin'), id_of('field'), $user_id, $field->get_value('_field_move_temp_name'), $values);
        $source_table_fields[$k]->set_value('_new_field_id', $id);
        $query_parts[] = '`' . reason_sql_string_escape($destination_table) . '`.`' . reason_sql_string_escape($field->get_value('_field_move_temp_name')) . '` = `' . reason_sql_string_escape($source_table) . '`.`' . reason_sql_string_escape($field->get_value('name')) . '`';
    }
    // copy content of old fields to new fields
    $q = 'UPDATE `' . reason_sql_string_escape($destination_table) . '`, `' . reason_sql_string_escape($source_table) . '`, `entity` SET ' . implode(' , ', $query_parts) . ' WHERE `' . reason_sql_string_escape($destination_table) . '`.`id` = `' . reason_sql_string_escape($source_table) . '`.`id` AND `' . reason_sql_string_escape($destination_table) . '`.`id` = `entity`.`id` AND `entity`.`type` = "' . reason_sql_string_escape($type_id) . '";';
    db_query($q, 'Attempt to move data between fields');
    // zap source table's type-to-table relationship for this type
    $conditions = array('entity_a' => $type_id, 'entity_b' => $source_table_entity->id(), 'type' => relationship_id_of('type_to_table'));
    delete_relationships($conditions);
    // create new field-to-table relationship for new fields and update field names in new table -- remove temp flag
    foreach ($source_table_fields as $field) {
        create_relationship($field->get_value('_new_field_id'), $destination_table_entity->id(), relationship_id_of('field_to_entity_table'));
        $q = 'ALTER TABLE `' . reason_sql_string_escape($destination_table) . '` CHANGE ' . reason_sql_string_escape($field->get_value('_field_move_temp_name')) . ' ' . reason_sql_string_escape($field->get_value('name')) . ' ' . $field->get_value('db_type');
        db_query($q, 'Unable to change field name of ' . $field->get_value('_field_move_temp_name') . ' in reason_move_table_fields()');
        reason_update_entity($field->get_value('_new_field_id'), $user_id, array('name' => $field->get_value('name')), false);
    }
    // delete the rows from the source table
    $q = 'DELETE `' . reason_sql_string_escape($source_table) . '` FROM `' . reason_sql_string_escape($source_table) . '`, `entity` WHERE `' . reason_sql_string_escape($source_table) . '`.`id` = `entity`.`id` AND `entity`.`type` = "' . reason_sql_string_escape($type_id) . '"';
    db_query($q, 'Attempt to delete rows from ' . $source_table . ' in reason_move_table_fields()');
    get_entity_tables_by_id($type_id, false);
    return true;
}
Пример #13
0
/**
 * Gets all the relationship info about an allowable relationship
 * @param int $r_id id in ar table
 * @return mixed
 */
function reason_get_allowable_relationship_info($alrel_id)
{
    $cache = array();
    if (!isset($cache[$alrel_id])) {
        $q = 'SELECT * FROM `allowable_relationship` WHERE `id` = "' . reason_sql_string_escape($alrel_id) . '"';
        $r = db_query($q, 'error getting relationship info');
        $cache[$alrel_id] = mysql_fetch_array($r, MYSQL_ASSOC);
    }
    return $cache[$alrel_id];
}
Пример #14
0
/**
 * Header the browser to the current location of the most recent page
 * that occupied a given URL
 *
 * How it works:
 *
 * 1. Looks for the URL in the URL_history table. 
 *
 * 2. If there is no URL, send a 404 header. 
 *    If there are URLs, send a 301 header and a Location header to the
 *    location of the live page that most recent inhabited that URL.
 *
 * Important: Because it may attempt to header the client to a different URL, 
 * this method must be called before any output is started, or in the context
 * of output buffering. 
 *
 * @param string $request_uri a URL relative to the host root (e.g. /foo/bar/)
 * @return NULL
 *
 * @todo modify to make multidomain safe
 */
function check_URL_history($request_uri)
{
    $url_arr = parse_URL($request_uri);
    // This catches links that might not have had a trailing slash
    // pages always have a trailing slash in the db
    $URL = '/' . trim_slashes($url_arr['path']) . '/';
    $URL = str_replace('//', '/', $URL);
    $query_string = !empty($url_arr['query']) ? '?' . $url_arr['query'] : '';
    $query = 'SELECT * FROM URL_history WHERE url ="' . reason_sql_string_escape($URL) . '" ORDER BY timestamp DESC';
    $results = db_query($query);
    $num_results = mysql_num_rows($results);
    if (mysql_num_rows($results) > 0) {
        while ($row = mysql_fetch_array($results)) {
            $page_id = $row['page_id'];
            $page = new entity($page_id);
            if (reason_is_entity($page, 'minisite_page') && $page->get_value('state') == 'Live' && ($redir = @reason_get_page_url($page))) {
                if ($redir == $request_uri) {
                    //Could potentially update rewrites here, solving most times this happens, perhaps.
                    trigger_error("A page should exist here, but apparently does not at the moment. A web administrator may need to run URL updating on this site.");
                } else {
                    header('Location: ' . $redir . $query_string, true, 301);
                    exit;
                }
            }
        }
    }
    // if we have gotten this far and not found a URL lets send a 404
    http_response_code(404);
}
Пример #15
0
/**
 * Get the Reason entity that represents the current user, if one exists
 *
 * @return mixed Reason entity or false (if no user logged in or if logged-in user does not have Reason entity)
 */
function reason_get_current_user_entity()
{
    static $user;
    if (!isset($user)) {
        if ($username = reason_check_authentication()) {
            $es = new entity_selector();
            $es->add_type(id_of('user'));
            $es->add_relation('entity.name = "' . reason_sql_string_escape($username) . '"');
            $es->set_num(1);
            $result = $es->run_one();
            if (!empty($result)) {
                $user = current($result);
            }
        }
        if (empty($user)) {
            $user = false;
        }
    }
    return $user;
}
Пример #16
0
    /**
     * We have many many dups ... this is basically because we've been updating ALL the timestamps
     */
    function clean_duplicate_values()
    {
        $num_to_process = 500;
        $query = 'SELECT `id`, `page_id`, `url`, `timestamp`, `deleted`, COUNT( * ) 
				FROM `URL_history` 
				GROUP BY `page_id`, `url`, `timestamp`
				HAVING COUNT( * ) >1
				ORDER BY `id` DESC';
        $result = db_query($query, 'error in query');
        $mycount = mysql_num_rows($result);
        if (mysql_num_rows($result) > 0) {
            if ($this->mode == 'test') {
                echo '<p>Would delete a chunk (maybe all) of the ' . $mycount . ' urls that have duplicate entries in the table.</p>';
            } elseif ($this->mode == 'run') {
                echo '<p>There are ' . $mycount . ' urls that appear to have duplicate entries. Please keep running this script until the number is 0. We
			             only process ' . $num_to_process . ' per run to minimize load on the database</p>';
                $counter = 0;
                while ($row = mysql_fetch_assoc($result)) {
                    $counter++;
                    if ($counter == $num_to_process) {
                        break;
                    } else {
                        // lets delete the copies from the DB except for the id we selected
                        $id = $row['id'];
                        $page_id = $row['page_id'];
                        $timestamp = $row['timestamp'];
                        $url = reason_sql_string_escape($row['url']);
                        $qry = 'SELECT id FROM URL_history where id != ' . $id . ' AND page_id = ' . $page_id . ' AND url = "' . $url . '" AND timestamp = ' . $timestamp;
                        $daresult = db_query($qry);
                        if ($daresult) {
                            while ($myrow = mysql_fetch_assoc($daresult)) {
                                $todelete[] = $myrow['id'];
                            }
                        }
                    }
                }
                if (isset($todelete)) {
                    $deleter_sql = 'DELETE FROM URL_history WHERE id IN ("' . implode('","', $todelete) . '")';
                    db_query($deleter_sql, 'Could not delete rows from URL_history');
                    echo '<p>Deleted some of the URLs that contain duplicate entries ... more could remain.</p>';
                }
            }
        } else {
            echo '<p>There are not duplicates in the URL_history table that need deletion - you may have already run this script</p>';
            return true;
        }
        return false;
    }
Пример #17
0
 function _blurb_also_appears_on($blurb)
 {
     $es = new entity_selector();
     $es->add_type(id_of('minisite_page'));
     $es->add_left_relationship($blurb->id(), relationship_id_of('minisite_page_to_text_blurb'));
     $es->add_relation('entity.id != "' . reason_sql_string_escape($this->page_id) . '"');
     return $es->run_one();
 }
Пример #18
0
 function add_complete($unique_name, $name, $css = array(), $template_name, $user_id)
 {
     if ($this->get_id()) {
         trigger_error('add_complete method may only be called on a reasonTheme that does not yet have an ID assigned.');
         return false;
     }
     $all_ok = true;
     $output = '<h4>Adding theme: ' . $unique_name . '</h4>';
     $output .= '<ol>';
     if (empty($template_name)) {
         $template_name = 'default';
     }
     if (empty($name)) {
         $name = prettify_string($unique_name);
     }
     $template = get_template_by_name($template_name);
     if (empty($template)) {
         $all_ok = false;
         $template_id = reason_add_template($template_name);
         if (!empty($template_id)) {
             $template = new entity($template_id);
             $output .= '<li>Template created (' . $template_name . ', id ' . $template->id() . ')</li>';
         } else {
             $output .= '<li>Unable to create template ' . $template_name . '. The template file may not be placed correctly.';
             if ($this->test_mode) {
                 $output .= ' Would abort theme addition.';
             } else {
                 $output .= ' Aborting theme addition.';
             }
             $output .= '</li></ol>';
             return array('success' => false, 'report' => $output);
         }
     } else {
         $output .= '<li>Template found (' . $template_name . ', id ' . $template->id() . ')</li>';
     }
     $theme_id = id_of($unique_name);
     if (!$theme_id) {
         $all_ok = false;
         $output .= '<li>Theme with unique name ' . $unique_name . ' needs to be created</li>';
         if (!$this->test_mode) {
             $theme_id = $this->create($unique_name, $name, $template->id(), $user_id);
             if (!$theme_id) {
                 $output .= '<li>Theme ' . $name . ' unable to be created; aborting theme addition</li></ol>';
                 return array('success' => false, 'report' => $output);
             } else {
                 $output .= '<li>Theme with unique name ' . $unique_name . ' created</li>';
             }
         } else {
             $output .= '<li>Would have attempted to create theme entity.</li>';
         }
     } else {
         $this->set_id($theme_id);
     }
     if ($this->get_template_id() != $template->id()) {
         $all_ok = false;
         $output .= '<li>Current theme template id (' . $this->get_template_id() . ') not the same as specified in update (' . $template->id() . ').</li>';
         if ($this->test_mode) {
             $output .= '<li>Would have set theme template to be id ' . $template->id() . '</li>';
         } else {
             if ($this->attach_template($template->id())) {
                 $output .= '<li>Attached template id ' . $template->id() . ' to ' . $unique_name . '.</li>';
             } else {
                 $output .= '<li>Unable to attach template for some reason. Aborting theme addition.</li>';
                 return array('success' => false, 'report' => $output);
             }
         }
     }
     $retrieved_css_entities = array();
     foreach ($css as $css_name => $css_info) {
         $output .= '<li>CSS: ' . $css_name . '<ol>';
         if (empty($css_info['url'])) {
             $output .= '<li>' . $css_name . ' has no url specified; skipping this css item</li></ol></li>';
             continue;
         } else {
             $css_url = $css_info['url'];
         }
         if (empty($retrieved_css_entities[$css_url])) {
             $es = new entity_selector();
             $es->add_type(id_of('css'));
             $es->add_relation('url = "' . reason_sql_string_escape($css_url) . '"');
             $es->set_num(1);
             $css_ents = $es->run_one();
             if (!empty($css_ents)) {
                 $retrieved_css_entities[$css_url] = current($css_ents);
             }
         }
         if (empty($retrieved_css_entities[$css_url])) {
             $all_ok = false;
             //create css & get id of css
             if ($this->test_mode) {
                 $output .= '<li>Would have created a css entity for ' . $css_url . ' and attached it to the theme</li>';
             } else {
                 $css_id = reason_create_entity(id_of('master_admin'), id_of('css'), $user_id, $css_name, $css_info);
                 if ($css_id) {
                     $output .= '<li>Created a css entity for ' . $css_url . '</li>';
                 } else {
                     $output .= '<li>Tried to create a css entity for ' . $css_url . ' but was unsuccessful. Aborting creation of this css item.</li>';
                     continue;
                 }
             }
         } else {
             $css_id = $retrieved_css_entities[$css_url]->id();
             $output .= '<li>A css entity exists for ' . $css_url . ' at Reason id ' . $css_id . '</li>';
         }
         if (!empty($css_id)) {
             // attach css
             $attached_css = $this->get_css();
             if (empty($attached_css[$css_id])) {
                 $all_ok = false;
                 if ($this->test_mode) {
                     $output .= '<li>Would have attached css at ' . $css_url . ' to ' . $unique_name . '.</li>';
                 } else {
                     if ($this->attach_css($css_id)) {
                         $output .= '<li>Attached css at ' . $css_url . ' to ' . $unique_name . '.</li>';
                     } else {
                         $output .= '<li>Unable to attach css at ' . $css_url . ' to ' . $unique_name . '; perhaps there is an unknown problem.</li>';
                     }
                 }
             } else {
                 $output .= '<li>Css at ' . $css_url . ' already attached to ' . $unique_name . '</li>';
             }
             $output .= '</ol></li>';
         }
     }
     if ($all_ok) {
         $output .= '<li><strong>Everything OK.</strong> The theme ' . $unique_name . ' appears to be set up correctly.  No database changes are needed.</li>';
     }
     $output .= '</ol>';
     return array('success' => true, 'report' => $output);
 }
Пример #19
0
 function get_search_term_for_query()
 {
     return str_replace('_', '\\_', reason_sql_string_escape($this->get_search_term()));
 }
Пример #20
0
function add_indexes_b3_to_b4($test_mode = true)
{
    echo '<h3>Adding indexes</h3>';
    echo '<ul>';
    foreach (get_indexes_to_add_b3_to_b4() as $table => $fields) {
        $handle = db_query('SHOW INDEX FROM `' . reason_sql_string_escape($table) . '`');
        $results = array();
        while ($row = mysql_fetch_assoc($handle)) {
            $results[] = $row['Column_name'];
        }
        foreach ($fields as $field) {
            if (in_array($field, $results)) {
                echo '<li>' . $table . '.' . $field . ' is already indexed. No need to do anything.</li>';
            } else {
                if ($test_mode) {
                    echo '<li>Would have added index on ' . $table . '.' . $field . '.</li>';
                } else {
                    if (db_query('ALTER TABLE `' . reason_sql_string_escape($table) . '` ADD INDEX ( `' . reason_sql_string_escape($field) . '` )')) {
                        echo '<li>Successfully added index on ' . $table . '.' . $field . '.</li>';
                    } else {
                        echo '<li>Attempted to add index on ' . $table . '.' . $field . ', but failed.</li>';
                    }
                }
            }
        }
    }
    echo '</ul>';
}
/**
 *  Relationship Finder
 *
 *  A function to find the id of a relationship given two entities' unique names. 
 *  relationship_finder will return false if zero, or multiple relationships are found. 
 *  
 *  For example: 
 *  
 *  echo relationship_finder( 'site', 'minisite_page', 'owns' ) . '<br />';
 *  
 *  gives you:
 *  78
 *
 * @param mixed $entity_a The unique name, id, or entity of the type on the "A" side of the relationship
 * @param mixed $entity_b The unique name, id, or entity of the type on the "B" side of the relationship
 * @param string $name the name of the relationship
 * @return mixed The ID of the allowable relationship or NULL if not found
 * @deprecated since Reason 4.2 just use relationship_id_of with the name
 */
function relationship_finder($entity_a, $entity_b, $name = 'owns')
{
    if (is_object($entity_a)) {
        $a_id = $entity_a->id();
    } elseif (is_numeric($entity_a)) {
        $a_id = (int) $entity_a;
    } else {
        $a_id = id_of($entity_a);
    }
    if (is_object($entity_b)) {
        $b_id = $entity_b->id();
    } elseif (is_numeric($entity_b)) {
        $b_id = (int) $entity_b;
    } else {
        $b_id = id_of($entity_b);
    }
    $name = (string) $name;
    // if the name string passed in is simply "owns" or "borrows" and relationship uses unique relationship names, update the name we look for and trigger an error
    if (($name == 'owns' || $name == 'borrows') && reason_relationship_names_are_unique()) {
        $a = new entity($a_id);
        $b = new entity($b_id);
        $name = $a->get_value('unique_name') . '_' . $name . '_' . $b->get_value('unique_name');
        // this assumes unique names not cool
        trigger_error('The function relationship_finder was called to discover an owns or borrows relationship. The strings "owns" and "borrows"
			           are no longer used as relationship names. Calling this method is no longer necessary to find the relationship_id of owns
			           or borrows relationships. Use get_owns_relationship_id or get_borrows_relationship_id instead.');
    }
    if (empty($a_id)) {
        trigger_error('$entity_a (' . $entity_a . ') is not a valid unique name');
        return;
    }
    if (empty($b_id)) {
        trigger_error('$entity_b (' . $entity_b . ') is not a valid unique name');
        return;
    }
    if (empty($name)) {
        trigger_error('An entity name must be provided for relationship_finder to work');
        return;
    }
    $query = 'SELECT id FROM allowable_relationship WHERE ' . 'relationship_a="' . $a_id . '" ' . 'AND relationship_b="' . $b_id . '" ' . 'AND name="' . reason_sql_string_escape($name) . '"';
    $results = db_query($query);
    $num = mysql_num_rows($results);
    if ($num < 1) {
        //Relationship finder returned zero results.
        return false;
    } elseif ($num > 1) {
        //Relationship finder returned too many results!
        if (is_object($entity_a)) {
            $a_name = $entity_a->get_value('name');
        } else {
            $a_name = $entity_a;
        }
        if (is_object($entity_b)) {
            $b_name = $entity_b->get_value('name');
        } else {
            $b_name = $entity_b;
        }
        trigger_error('Multiple relationships exist for "' . $a_name . '" to "' . $b_name . '" under name "' . $name . '"; returning only first result.');
    }
    $results = mysql_fetch_array($results);
    return (int) $results['id'];
}
 while (list($id, $d) = each($duplicates)) {
     //echo $d['count'].' '.$d['name'].'<br />';
     // find other IDs to delete
     $q = new DBSelector();
     reset($tables);
     while (list(, $t) = each($tables)) {
         $q->add_table($t);
         if ($t != 'entity') {
             $q->add_relation('entity.id = ' . $t . '.id');
         }
     }
     $q->add_relation('entity.id != ' . $id);
     reset($fields);
     while (list(, $f) = each($fields)) {
         if ($d[$f]) {
             $q->add_relation($f . ' = "' . reason_sql_string_escape($d[$f]) . '"');
         } else {
             $q->add_relation($f . ' IS NULL');
         }
     }
     $q->add_field('entity', 'id');
     //echo $q->get_query().'<br /><br />';
     $ids_to_delete = $q->run();
     //pray( $ids_to_delete );
     reset($ids_to_delete);
     while (list(, $id_to_delete) = each($ids_to_delete)) {
         $itd = $id_to_delete['id'];
         // delete id from each table
         reset($tables);
         while (list($key, $table) = each($tables)) {
             $q = "DELETE FROM {$table} WHERE id = '{$itd}'";
Пример #23
0
 function move_into_site($orphan_id, $types, $owner_site_id)
 {
     if (empty($this->_user_id)) {
         trigger_error('Must set user id before calling move_into_site()');
         return false;
     }
     if (!get_owner_site_id($orphan_id)) {
         $owns_rel_id = get_owns_relationship_id($types);
         // If there is an existing entry in the relationship table, delete it
         $q = 'DELETE FROM `relationship` WHERE `entity_b` = "' . reason_sql_string_escape($orphan_id) . '" AND `type` = "' . reason_sql_string_escape($owns_rel_id) . '"';
         $r = db_query($q, 'Unable to delete old owns relationship');
         // create new ownership entry
         create_relationship($owner_site_id, $orphan_id, $owns_rel_id);
     } else {
         trigger_error($orphan_id . ' not actually an orphan');
         return false;
     }
 }
Пример #24
0
 /**
  * Remove all locks from this entity
  *
  * Note that this method will return false in any case that does not
  * result in the locks being deleted, including:
  * 1. User does not have privs to do operation
  * 2. Entity has no locks to delete
  * 4. Database delete failed
  * 
  * @return boolean locks deleted
  * @todo add logging
  */
 public function remove_all_locks()
 {
     $user = $this->_get_current_user();
     if (empty($user) || !reason_user_has_privs($user->id(), 'manage_locks')) {
         trigger_error('Rejecting attempt to remove all locks on entity ' . $this->_entity->id() . ' by unauthorized user: '******'DELETE FROM `entity_lock` WHERE `entity_id` = "' . reason_sql_string_escape($this->_entity->id()) . '"';
         // execute
         if (db_query($sql, 'Error removing all locks for entity ' . $this->_entity->id(), false)) {
             // log?
             self::$_raw_locks[$this->_entity->id()] = array();
             return true;
         }
     }
     return false;
 }
 protected function _add_table($table_name)
 {
     $es = new entity_selector();
     $es->add_type(id_of('content_table'));
     $es->add_relation('entity.name = "' . reason_sql_string_escape($table_name) . '"');
     $es->set_num(1);
     $results = $es->run_one();
     if (empty($results)) {
         create_reason_table($table_name, $this->_type_unique_name(), $this->user_id());
         $ret = '<p>Created the ' . $table_name . ' table.</p>';
     } else {
         $table_entity = current($results);
         create_relationship($this->_type_id(), $table_entity->id(), relationship_id_of('type_to_table'));
         $ret = '<p>Added the ' . $table_name . ' table to the ' . $this->_type_unique_name() . ' type.</p>' . "\n";
     }
     reason_include_once('classes/amputee_fixer.php');
     $fixer = new AmputeeFixer();
     $fixer->fix_amputees($this->_type_id());
     return $ret;
 }
Пример #26
0
 public function contains_any_events_after($date)
 {
     if (!$this->contains_any_events()) {
         return false;
     }
     if (!empty($this->known_upper_limit) && $this->known_upper_limit > $date) {
         //echo '<strong>contains_any_events_after</strong>: req.date ('.$date.') before kul ('.$this->known_lower_limit.'); ret true<br />';
         return true;
     } elseif (!empty($this->known_closest_date_after) && $this->known_closest_date_after < $date) {
         //echo '<strong>contains_any_events_after</strong>: req.date ('.$date.') after kcda ('.$this->known_closest_date_after.'); ret false<br />';
         return false;
     }
     $test_es = carl_clone($this->base_es);
     $test_es->set_num(1);
     $test_es->add_relation('event.last_occurence > "' . reason_sql_string_escape($date) . '"');
     $test_es->limit_fields();
     $test_es->exclude_tables_dynamically();
     $test_es->set_cache_lifespan($this->_get_cache_lifespan_meta());
     //$test_es->optimize('');
     //echo $test_es->get_one_query().'<br />';
     $test_results = $test_es->run_one();
     if (!empty($test_results)) {
         $result = current($test_results);
         //echo '<strong>contains_any_events_after</strong>: found event after req.date '.$date.'. id: '.$result->id().'; datetime: '.$result->get_value('datetime').'; ret true<br />';
         $this->known_upper_limit = substr($result->get_value('datetime'), 0, 10);
         return true;
     } else {
         //echo '<strong>contains_any_events_after</strong>: no events found after req.date ('.$date.'); ret false<br />';
         $this->known_closest_date_after = $date;
         return false;
     }
 }
Пример #27
0
 function get_field_id($table_name, $field_name)
 {
     $es = new entity_selector(id_of('master_admin'));
     $es->add_type(id_of('content_table'));
     $es->add_relation('entity.name = "' . reason_sql_string_escape($table_name) . '"');
     $es->set_num(1);
     $tables = $es->run_one();
     if (empty($tables)) {
         trigger_error('Unable to find table named ' . $table_name);
         return false;
     }
     $table = current($tables);
     $es = new entity_selector(id_of('master_admin'));
     $es->add_type(id_of('field'));
     $es->add_left_relationship($table->id(), relationship_id_of('field_to_entity_table'));
     $es->add_relation('entity.name = "' . reason_sql_string_escape($field_name) . '"');
     $es->set_num(1);
     $fields = $es->run_one();
     if (empty($fields)) {
         trigger_error('Unable to find table named ' . $field_name, ' in table named ' . $table_name);
         return false;
     }
     $field = current($fields);
     return $field->id();
 }
foreach ($types as $id => $type) {
    $type_names[$id] = $type->get_value('name');
}
$d = new DiscoSearcher();
$d->add_element('search_string');
$d->add_element('type', 'select_no_sort', array('options' => $type_names));
if (!empty($_REQUEST['type'])) {
    $d->set_value('type', $_REQUEST['type']);
}
if (!empty($_REQUEST['search_string'])) {
    $d->set_value('search_string', $_REQUEST['search_string']);
}
$d->actions = array('Search');
$d->run();
if (!empty($_REQUEST['search_string'])) {
    $sql_search_string = reason_sql_string_escape($_REQUEST['search_string']);
    $use_fields = array('id', 'name', 'last_modified');
    echo '<h2>Search results</h2>';
    $hit_count = 0;
    $txt = '';
    if (!empty($_REQUEST['type'])) {
        if (isset($types[$_REQUEST['type']])) {
            $only_type = $types[$_REQUEST['type']];
            $types = array($_REQUEST['type'] => $only_type);
        } else {
            $types = array();
            echo 'Not a type';
        }
    }
    foreach ($types as $type) {
        //echo $type->get_value('name').'<br />';
Пример #29
0
 function do_filters_search()
 {
     if (!empty($this->request['search'])) {
         $search_term = $this->request['search'];
         $regexp = '/(?:\\"(.+?)\\"|([^\\*\\"\\s]+))/';
         preg_match_all($regexp, $search_term, $matches);
         $search_term_array = array();
         foreach ($matches[1] as $chunk) {
             if (!empty($chunk)) {
                 $search_term_array[] = trim($chunk);
             }
         }
         foreach ($matches[2] as $chunk) {
             if (!empty($chunk)) {
                 $search_term_array[] = trim($chunk);
             }
         }
         $search_array = array();
         foreach ($search_term_array as $chunk) {
             $sub_search_array = array();
             foreach ($this->search_fields as $field) {
                 $table_field = '';
                 if (false === strpos($field, '.')) {
                     $table_field = table_of($field, $this->type);
                 }
                 if (empty($table_field)) {
                     $table_field = $field;
                 }
                 $sub_search_array[] = $table_field . ' LIKE "%' . strtr(reason_sql_string_escape($chunk), array('*' => '%')) . '%"';
             }
             $search_array[] = '(' . implode(' OR ', $sub_search_array) . ')';
         }
         if (!empty($search_array)) {
             $this->es->add_relation('(' . implode(' AND ', $search_array) . ')');
         }
     }
     foreach ($this->allowable_psearch_fields as $psearch_frag => $psearch_data) {
         if (!empty($this->request['search_' . $psearch_frag])) {
             $psearch_string = str_replace('*', '%', reason_sql_string_escape($this->request['search_' . $psearch_frag]));
             $this->es->add_relation('(' . $psearch_data['field'] . ' LIKE "' . $psearch_string . '")');
         }
     }
 }
Пример #30
0
 function set_user_prefs()
 {
     $myname = reason_check_authentication();
     if (!empty($myname)) {
         $popup_alert = 'no';
         $es = new entity_selector();
         $es->add_type(id_of('user'));
         $es->add_relation('entity.name = "' . reason_sql_string_escape($myname) . '"');
         $es->set_num(1);
         $users = $es->run_one();
         if (!empty($users)) {
             $user = current($es->run_one());
             $popup_alert = $user->get_value('user_popup_alert_pref');
         }
         $this->set('_user_popup_alert_pref', $popup_alert);
     }
 }