function init($args = array())
 {
     if ($this->params['force_secure'] && HTTPS_AVAILABLE && !on_secure_page()) {
         header('Location: ' . get_current_url(securest_available_protocol()));
         exit(0);
     }
     // Check for an existing database connection so we can restore it when we're done
     $this->db_conn = get_current_db_connection_name();
 }
예제 #2
0
 /**
  * Open connection to service
  * @access public
  */
 function open_conn()
 {
     if (get_current_db_connection_name() != $this->_conn_params['connectDBname']) {
         $this->prev_connection_name = get_current_db_connection_name();
         if ($this->_conn = connectDB($this->_conn_params['connectDBname'])) {
             return true;
         }
         return false;
     }
     return true;
 }
예제 #3
0
 /**
  * Run query and return array of results
  *
  * @param string $error_message
  * @param boolean $die_on_error
  * @return array
  */
 function run($error_message = '', $die_on_error = true)
 {
     $q = $this->get_query();
     if ($this->cache_lifespan) {
         $cache = new ObjectCache('db_selector_cache_' . get_current_db_connection_name() . '_' . $q, $this->cache_lifespan);
         $results =& $cache->fetch();
         if (false !== $results) {
             return $results;
         }
     }
     $results = array();
     $r = db_query($q, $error_message, $die_on_error);
     if ($r) {
         while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
             $results[] = $row;
         }
         mysql_free_result($r);
     }
     if (!empty($cache)) {
         $cache->set($results);
     }
     return $results;
 }
예제 #4
0
    protected function get_course_section_data($org_id = null)
    {
        echo "get_course_section_data {$org_id}\n";
        $data = array();
        $coursetable = 'course2014';
        $restore_conn = get_current_db_connection_name();
        connectDB('reg_catalog_new');
        mysql_set_charset('utf8');
        $found = false;
        $coursetableyear = 2014;
        $org_id_limit = $org_id ? ' AND SEC_SUBJECT="' . $org_id . '" ' : '';
        while ($coursetableyear > 2009) {
            $coursetable = 'course' . $coursetableyear;
            $query = 'SELECT s.*, description, title FROM IDM_CRS c, IDM_COURSE s
				JOIN ' . $coursetable . ' 
				ON ' . $coursetable . '.course_id = s.SEC_COURSE 
				AND (' . $coursetable . '.match_title IS NULL 
				OR ' . $coursetable . '.match_title = s.SEC_SHORT_TITLE)
				WHERE s.SEC_COURSE = c.COURSES_ID AND
				(CRS_END_DATE IS NULL OR CRS_END_DATE > NOW()) 
				AND SEC_START_DATE > "2009-09-01 00:00:00" ' . $org_id_limit . '
				ORDER BY SEC_NAME';
            if ($result = mysql_query($query)) {
                while ($row = mysql_fetch_assoc($result)) {
                    if (isset($row['SEC_SUBJECT']) && empty($data[$row['COURSE_SECTIONS_ID']])) {
                        if ($row['SEC_SUBJECT'] == 'OCP' || $row['SEC_SUBJECT'] == 'NORW') {
                            continue;
                        }
                        if (strpos($row['SEC_NO'], 'WL') !== false) {
                            continue;
                        }
                        //if (strpos($row['SEC_TERM'], 'SU') !== false) continue;
                        $data[$row['COURSE_SECTIONS_ID']] = $row;
                    }
                }
            } else {
                $this->errors[] = mysql_error();
            }
            $coursetableyear--;
        }
        connectDB($restore_conn);
        return $data;
    }
 /**
  * Open connection to service
  * @access public
  */
 function open_conn()
 {
     $this->prev_connection_name = get_current_db_connection_name();
     if (connectDB(REASON_DB)) {
         return true;
     }
     return false;
 }
예제 #6
0
 /**
  * Private function to handle database connections - only makes a new connection when needed
  * @access private
  */
 function _disco_conn($bool)
 {
     //static $orig;
     //static $curr;
     if (empty($this->orig)) {
         $this->orig = $this->curr = get_current_db_connection_name();
     }
     if ($bool && $this->curr != $this->db_conn) {
         connectDB($this->db_conn);
         $this->curr = $this->db_conn;
     } elseif (!$bool && $this->curr != $this->orig) {
         connectDB($this->orig);
         $this->curr = $this->orig;
     }
 }
예제 #7
0
/**
 * Find out what database is currently in use
 * @return string name of db
 */
function get_database_name()
{
    $conn_name = get_current_db_connection_name();
    $creds = get_db_credentials($conn_name);
    return $creds['db'];
}
예제 #8
0
	function perform_update($id, $values)
	{
		if (!get_current_db_connection_name()) connectDB($this->get_db_conn());
		$restore_conn = ($this->get_db_conn() != get_current_db_connection_name()) ? get_current_db_connection_name() : false;
		if ($restore_conn) connectDB($this->get_db_conn());
		$result = $GLOBALS['sqler']->update_one( $this->get_table_name(), $values, $id );
		if ($restore_conn) connectDB($restore_conn);
		return $result;
	}
예제 #9
0
 function init($args = array())
 {
     parent::init($args);
     if (!$this->can_run()) {
         trigger_error('thor needs a db connection name (thor_db_conn_name)');
         return;
     }
     include_once CARL_UTIL_INC . 'db/db.php';
     $this->original_db_conn_name = get_current_db_connection_name();
     $this->set($this->value);
     return true;
     // if $this->value doesn't contain an xml declaration,assume it's a tmp_id
     if (strpos($this->value, '<' . '?xml') === false) {
         prp('in init, matches number');
         prp($this->value, 'this->value, in init (early)');
         $this->tmp_id = $this->value;
         // Use file('getXML.php'), instead of a direct call to
         // the database, because connecting to the test
         // database here seems to screw up the connection to
         // cms. Possibly only one mysql link can be open at
         // once.
         $this->value = implode("\n", file('http://' . HTTP_HOST_NAME . THOR_HTTP_PATH . 'getXML.php?tmp_id=' . $this->tmp_id));
         prp($this->value, 'this->value, in init (late)');
     } else {
         connectDB($this->thor_db_conn_name);
         include_once CARL_UTIL_INC . 'db/sqler.php';
         $sqler = new SQLER();
         $sqler->insert('thor', array('content' => $this->value));
         $this->tmp_id = mysql_insert_id();
         connectDB($this->original_db_conn_name);
     }
 }
예제 #10
0
 /**
  * Private function to handle database connections - only makes a new connection when needed
  * @access private
  */
 function _cache_conn($bool)
 {
     static $orig;
     static $curr;
     if (empty($orig)) {
         $orig = $curr = get_current_db_connection_name();
     }
     if ($bool && $curr != OBJECT_CACHE_DB_CONN) {
         connectDB(OBJECT_CACHE_DB_CONN);
         $curr = OBJECT_CACHE_DB_CONN;
     } elseif (!$bool && $curr != $orig) {
         connectDB($orig);
         $curr = $orig;
     }
 }
예제 #11
0
 /**
  * Runs one query for the ES.  If type is empty, it uses the first type by default.
  * This is often called without paramaters in code for front end stuff.
  * <code>
  * $es = new entity_selector( $site_id );
  * $es->add_type( $type_id );
  * $results = $es->run_one();
  * </code>
  * @param int $type type_id (or blank for default)
  * @param string $status Either Live, Pending, Archived, or All ... (optional)
  * @param string $error optional error message
  * @return array
  */
 function run_one($type = '', $status = 'Live', $error = 'run_one error')
 {
     if (!$type) {
         if (isset($this->type[0]) && $this->type[0]) {
             $type = $this->type[0];
         } else {
             trigger_error('Entity Selector: No type available. Try using the method add_type($type_id) before calling run_one(), or call run_one() with the type id as the first argument.');
             return array();
         }
     }
     $query = $this->get_one_query($type, $status);
     $factory =& $this->get_entity_factory();
     if ($this->cache_lifespan) {
         $factory_class = $factory ? get_class($factory) : '';
         //echo '<p>caching '.$this->cache_lifespan.' secs</p>';
         $cache = new ObjectCache('entity_selector_cache_' . get_current_db_connection_name() . '_' . $this->_enable_multivalue_results . '_' . $factory_class . '_' . $query, $this->cache_lifespan);
         $results =& $cache->fetch();
         if (false !== $results) {
             //echo '<p>Cache hit</p>';
             return $results;
         }
         //echo '<p>Cache miss</p>';
     }
     $results = array();
     $r = db_query($query, $this->description . ': ' . $error);
     while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
         //pray ($row);
         if ($this->_enable_multivalue_results && isset($results[$row['id']])) {
             $prev_val = $new_val = $key = $val = '';
             $e = $results[$row['id']];
             foreach ($row as $key => $val) {
                 $cur_value = $e->get_value($key);
                 if (is_array($cur_value)) {
                     if (!in_array($val, $cur_value) && !empty($val)) {
                         $cur_value[] = $val;
                         $e->set_value($key, $cur_value);
                     }
                 } elseif ($cur_value != $val && !empty($val)) {
                     if (empty($cur_value)) {
                         $e->set_value($key, $val);
                     } else {
                         $e->set_value($key, array($cur_value, $val));
                     }
                 }
             }
         } else {
             if ($factory) {
                 $e = $factory->get_entity($row);
             } else {
                 $e = new entity($row['id']);
             }
             $e->_values = $row;
         }
         $results[$row['id']] = $e;
     }
     mysql_free_result($r);
     if (!empty($cache)) {
         $cache->set($results);
     }
     return $results;
 }
예제 #12
0
/** db_query( $query, $error_message = '', $die_on_error = true ) {{{
 *	Wrapper function for querying the database
 *
 *	Wraps up extra functionality for handling queries.
 *	- easy handling of errors.
 *	- query tracking
 *	- query reporting
 *
 *	@param	$query	the query to run
 *	@param	$error_message	the custom error message
 *	@param	$die_on_error	boolean variable that determines whether to die on an error
 *	@return	query result if query succeeds or false if query fails.
 */
function db_query($query, $error_message = '', $die_on_error = true)
{
    // keep track of all queries
    static $queries;
    static $distinct_queries;
    static $distinct_errors;
    static $first_run = true;
    if ($first_run) {
        if (isset($GLOBALS['_db_query_first_run_connection_name']) && !get_current_db_connection_name()) {
            connectDB($GLOBALS['_db_query_first_run_connection_name']);
        }
        $first_run = false;
    }
    if (!isset($queries) or empty($queries)) {
        $queries = array();
    }
    if (!isset($distinct_errors) or empty($distinct_errors)) {
        $distinct_errors = array();
    }
    if (!isset($distinct_queries) or empty($distinct_queries)) {
        $distinct_queries = array();
    }
    $queries[] = array('q' => $query, 'error' => $error_message);
    if (!isset($distinct_queries[$query])) {
        $distinct_queries[$query] = 0;
    }
    $distinct_queries[$query]++;
    if (!isset($distinct_errors[$error_message])) {
        $distinct_errors[$error_message] = 0;
    }
    $distinct_errors[$error_message]++;
    switch ($query) {
        // profiling and reporting cases
        case 'GET NUM QUERIES':
            return count($queries);
            break;
        case 'GET QUERIES':
            return $queries;
            break;
        case 'REPORT DISTINCT QUERIES':
            arsort($distinct_queries);
            pray($distinct_queries);
            break;
        case 'REPORT DISTINCT ERRORS':
            arsort($distinct_errors);
            pray($distinct_errors);
            break;
        case 'REPORT':
            echo '<br /><br />';
            echo '<strong>Queries run through db_query():</strong> ' . count($queries) . '<br />';
            echo '<strong>Queries:</strong><br />';
            pray($queries);
            break;
            // query case
        // query case
        default:
            // run the query
            if ($r = mysql_query($query)) {
                return $r;
            } else {
                global $PHP_SELF;
                if (empty($error_message)) {
                    $error_message = 'Bad db query.';
                }
                $body = $error_message . '<br />';
                $body .= 'Query: "' . str_replace("\n", ' ', $query) . '"<br />';
                $body .= 'Error: "' . mysql_error() . '" (errno: "' . mysql_errno() . '")';
                $errorlevel = MEDIUM;
                if ($die_on_error) {
                    $errorlevel = EMERGENCY;
                }
                trigger_error(str_replace("\n", '', nl2br($body)), $errorlevel);
                if ($die_on_error) {
                    die;
                }
                return false;
            }
            break;
    }
}
예제 #13
0
 function delete_table()
 {
     if ($this->get_thor_table() && $this->table_exists()) {
         $this->delete_file_storage_for_form();
         $sql = $this->get_delete_table_sql();
         if (!get_current_db_connection_name()) {
             connectDB($this->get_db_conn());
         }
         $reconnect_db = get_current_db_connection_name() != $this->get_db_conn() ? get_current_db_connection_name() : false;
         if ($reconnect_db) {
             connectDB($this->get_db_conn());
         }
         $res = db_query($sql);
         if ($reconnect_db) {
             connectDB($reconnect_db);
         }
         // reconnect to default DB
         return true;
     } else {
         $error = !$this->get_thor_table() ? 'You need to define the table name using set_thor_table before calling create_table' : 'The table you are trying to delete does not exist!';
         trigger_error($error);
         return NULL;
     }
 }
예제 #14
0
/**
 * Escape a string for use in a Reason db sql statement
 *
 * Note that calling this function *may* prompt the creation of a Reason database connection
 * when the $ensure_db_connection parameter is set to true. (true is the default).
 *
 * @param string $str
 * @param boolean $ensure_db_connection Create a Reason DB connection if none exists to ensure proper escaping
 * @return string
 */
function reason_sql_string_escape($str, $ensure_db_connection = true)
{
    // If there is no connection, connect to the reason db
    if ($ensure_db_connection && get_current_db_connection_name() == '') {
        connectDB(REASON_DB);
    }
    return mysql_real_escape_string($str);
}
예제 #15
0
 function init($args = array())
 {
     parent::init($args);
     // $attachPoints = array('init','on_first_time','on_every_time','pre_show_form','post_show_form','no_show_form','pre_error_check_actions','run_error_checks','post_error_check_actions','post_error_checks','process','where_to');
     // foreach ($attachPoints as $ap) { $this->parentContentManager->add_callback(array($this, 'discoEventTester_' . $ap), $ap); }
     // $this->parentContentManager->add_callback(array($this, 'discoEventTester'), "on_first_time");
     // $this->parentContentManager->add_callback(array($this, 'discoEventTester2'), "on_every_time");
     // echo "<HR>INITING FLASH.PHP<HR>";
     // $this->ensure_temp_db_table_exists();
     if (!$this->can_run()) {
         trigger_error('thor needs a db connection name (thor_db_conn_name)');
         return;
     }
     include_once CARL_UTIL_INC . 'db/db.php';
     $this->original_db_conn_name = get_current_db_connection_name();
     $this->set($this->value);
     return true;
     /*
     		// if $this->value doesn't contain an xml declaration,assume it's a tmp_id
     		if ( strpos($this->value, '<' . '?xml') === false )
     		{
     			prp('in init, matches number');
     			prp($this->value, 'this->value, in init (early)');
     			$this->tmp_id = $this->value;
     			// Use file('getXML.php'), instead of a direct call to
     			// the database, because connecting to the test
     			// database here seems to screw up the connection to
     			// cms. Possibly only one mysql link can be open at
     			// once.
     			$this->value = implode("\n", file('http://'.HTTP_HOST_NAME.THOR_HTTP_PATH.'getXML.php?tmp_id=' . $this->tmp_id));
     			prp($this->value, 'this->value, in init (late)');
     		}
     		// otherwise, assume $this->value contains the XML
     		else
     		{
     			connectDB($this->thor_db_conn_name);
     			include_once(CARL_UTIL_INC . 'db/sqler.php');
     			$sqler = new SQLER;
     			$sqler->insert('thor', Array('content' => $this->value));
     			$this->tmp_id = mysql_insert_id();
     			connectDB($this->original_db_conn_name);
     		}
     */
 }
예제 #16
0
 /**
  * Private function to handle database connections - only makes a new connection when needed
  * @access private
  */
 function _cache_conn($bool)
 {
     static $orig;
     static $curr;
     if (empty($orig)) {
         $orig = $curr = get_current_db_connection_name();
     }
     if ($bool && $curr != $this->get_db_conn()) {
         connectDB($this->get_db_conn());
         $curr = $this->get_db_conn();
     } elseif (!$bool && $curr != $orig) {
         connectDB($orig);
         $curr = $orig;
     }
 }
예제 #17
0
 function set_db_conn($db_conn)
 {
     $orig = get_current_db_connection_name();
     $this->_db_conn = $db_conn;
     $this->_orig_db_conn = $orig ? $orig : $db_conn;
 }