function importTermID($importTermID)
{
    global $error, $importLog, $tempDBName, $targetDBName, $sourceDBID;
    if (!$importTermID) {
        return "";
    }
    //the source term we want to import
    $term = mysql_fetch_assoc(mysql_query("select * from " . $tempDBName . ".defTerms where trm_ID = " . $importTermID));
    if (!$term || @$term['trm_ID'] != $importTermID) {
        // log the problem and return an empty string
        $error = true;
        makeLogEntry("<b>Error</b> Importing Term", $importTermID, " doesn't exist in source database");
        if (mysql_error()) {
            makeLogEntry("<b>Error</b> Importing Term", $importTermID, "SQL error importing term - " . mysql_error());
        }
        return "";
    } else {
        // If term has originating DBID 0, set it to the DBID from the DB it is being imported from
        if ($term["trm_OriginatingDBID"] == 0 || $term["trm_OriginatingDBID"] == "") {
            $term["trm_OriginatingDBID"] = $sourceDBID;
            $term["trm_IDInOriginatingDB"] = $importTermID;
        }
        // Check wether this term is already imported
        $resExistingTrm = mysql_query("select trm_ID from " . $targetDBName . ".defTerms " . "where trm_OriginatingDBID = " . $term["trm_OriginatingDBID"] . " AND trm_IDInOriginatingDB = " . $term["trm_IDInOriginatingDB"]);
        // Term is in target DB so return translated term ID
        if (mysql_num_rows($resExistingTrm) > 0) {
            $existingTermID = mysql_fetch_array($resExistingTrm);
            return $existingTermID[0];
        } else {
            // If parent term exist import  it first and use the save the parentID
            $sourceParentTermID = $term["trm_ParentTermID"];
            if ($sourceParentTermID != "" && $sourceParentTermID != 0) {
                $localParentTermID = importTermID($sourceParentTermID);
                // TODO: check that the term imported correctly
                if ($localParentTermID == "" || $localParentTermID == 0) {
                    makeLogEntry("<b>Error</b> Importing Term", $sourceParentTermID, ". Error importing parent term for term ID ={$importTermID} .");
                    return "";
                } else {
                    $term["trm_ParentTermID"] = $localParentTermID;
                    makeLogEntry("Importing Term", $sourceParentTermID, "as #{$localParentTermID}");
                }
            } else {
                unset($term["trm_ParentTermID"]);
            }
            $selfInverse = false;
            if ($term["trm_InverseTermId"] == $term["trm_ID"]) {
                $selfInverse = true;
            }
            $inverseSourceTrmID = $term["trm_InverseTermId"];
            // Import the term
            unset($term["trm_ID"]);
            unset($term["trm_ChildCount"]);
            unset($term["trm_InverseTermId"]);
            $term["trm_AddedByImport"] = 1;
            if (@$term["trm_Status"] == null) {
                $term["trm_Status"] = "open";
            }
            $term["trm_Label"] = mysql_escape_string($term["trm_Label"]);
            $term["trm_Description"] = mysql_escape_string($term["trm_Description"]);
            $term["trm_NameInOriginatingDB"] = mysql_escape_string($term["trm_NameInOriginatingDB"]);
            mysql_query("INSERT INTO " . $targetDBName . ".defTerms (" . implode(", ", array_keys($term)) . ") " . "VALUES ('" . implode("', '", array_values($term)) . "')");
            // Write the insert action to $logEntry, and set $error to true if one occurred
            if (mysql_error()) {
                $error = true;
                makeLogEntry("<b>Error</b> Importing Term", $importTermID, "MySQL error importing term -" . mysql_error());
                return "";
            } else {
                $newTermID = mysql_insert_id();
                makeLogEntry("Importing Term", $importTermID, "as #{$newTermID}");
            }
            //handle inverseTerm if there is one
            if ($inverseSourceTrmID && $inverseSourceTrmID != "") {
                if ($selfInverse) {
                    $localInverseTermID = $newTermID;
                } else {
                    $localInverseTermID = importTermID($inverseSourceTrmID);
                }
                // If there is an inverse term then update this term with it's local ID
                mysql_query("UPDATE " . $targetDBName . ".defTerms SET trm_InverseTermId=" . $localInverseTermID . " where trm_ID=" . $newTermID);
            }
            return $newTermID;
        }
    }
}
Beispiel #2
0
/**
 * 	Do PM DB maintenance
 *	@param array $opts of tasks key = sent|rec|blocked|expired  (one or more present). ATM value not used
 *	@return array where key is message type (E_MESSAGE_SUCCESS|E_MESSAGE_ERROR|E_MESSAGE_INFO etc), data is array of messages of that type (key = timestamp)
 */
function doMaint($opts, $pmPrefs)
{
    if (!count($opts)) {
        return array(E_MESSAGE_ERROR => array(ADLAN_PM_66));
    }
    $results = array(E_MESSAGE_INFO => array(ADLAN_PM_67));
    // 'Maintenance started' - primarily for a log entry to mark start time
    $logResults = array();
    $e107 = e107::getInstance();
    $e107->admin_log->log_event('PM_ADM_04', implode(', ', array_keys($opts)));
    $pmHandler = new private_message($pmPrefs);
    $db2 = new db();
    // Will usually need a second DB object to avoid over load
    $start = 0;
    // Use to ensure we get different log times
    if (isset($opts['sent'])) {
        $cnt = 0;
        if ($res = $db2->gen("SELECT pm.pm_id FROM `#private_msg` AS pm LEFT JOIN `#user` AS u ON pm.`pm_from` = `#user`.`user_id`\n\t\t\t\t\tWHERE (pm.`pm_read_del = 1) AND `#user`.`user_id` IS NULL")) {
            while ($row = $db2->fetch(MYSQL_ASSOC)) {
                if ($pmHandler->del($row['pm_id']) !== FALSE) {
                    $cnt++;
                }
            }
        }
        $start = time();
        $results[E_MESSAGE_SUCCESS][$start] = str_replace('--COUNT--', $cnt, ADLAN_PM_74);
    }
    if (isset($opts['rec'])) {
        $cnt = 0;
        if ($res = $db2->gen("SELECT pm.pm_id FROM `#private_msg` AS pm LEFT JOIN `#user` AS u ON pm.`pm_to` = `#user`.`user_id`\n\t\t\t\t\tWHERE (pm.`pm_sent_del = 1) AND `#user`.`user_id` IS NULL")) {
            while ($row = $db2->fetch(MYSQL_ASSOC)) {
                if ($pmHandler->del($row['pm_id']) !== FALSE) {
                    $cnt++;
                }
            }
        }
        $start = max($start + 1, time());
        $results[E_MESSAGE_SUCCESS][$start] = str_replace('--COUNT--', $cnt, ADLAN_PM_75);
    }
    if (isset($opts['blocked'])) {
        if ($res = $db2->gen("DELETE `#private_msg_block` FROM `#private_msg_block` LEFT JOIN `#user` ON `#private_msg_block`.`pm_block_from` = `#user`.`user_id`\n\t\t\t\t\tWHERE `#user`.`user_id` IS NULL")) {
            $start = max($start + 1, time());
            $results[E_MESSAGE_ERROR][$start] = str_replace(array('--NUM--', '--TEXT--'), array($this->sql->getLastErrorNum, $this->sql->getLastErrorText), ADLAN_PM_70);
        } else {
            $start = max($start + 1, time());
            $results[E_MESSAGE_SUCCESS][$start] = str_replace('--COUNT--', $res, ADLAN_PM_69);
        }
        if ($res = $db2->gen("DELETE `#private_msg_block` FROM `#private_msg_block` LEFT JOIN `#user` ON `#private_msg_block`.`pm_block_to` = `#user`.`user_id`\n\t\t\t\t\tWHERE `#user`.`user_id` IS NULL")) {
            $start = max($start + 1, time());
            $results[E_MESSAGE_ERROR][$start] = str_replace(array('--NUM--', '--TEXT--'), array($this->sql->getLastErrorNum, $this->sql->getLastErrorText), ADLAN_PM_70);
        } else {
            $start = max($start + 1, time());
            $results[E_MESSAGE_SUCCESS][$start] = str_replace('--COUNT--', $res, ADLAN_PM_68);
        }
    }
    if (isset($opts['expired'])) {
        $del_qry = array();
        $read_timeout = intval($pmPrefs['read_timeout']);
        $unread_timeout = intval($pmPrefs['unread_timeout']);
        if ($read_timeout > 0) {
            $timeout = time() - $read_timeout * 86400;
            $del_qry[] = "(pm_sent < {$timeout} AND pm_read > 0)";
        }
        if ($unread_timeout > 0) {
            $timeout = time() - $unread_timeout * 86400;
            $del_qry[] = "(pm_sent < {$timeout} AND pm_read = 0)";
        }
        if (count($del_qry) > 0) {
            $qry = implode(' OR ', $del_qry);
            $cnt = 0;
            if ($db2->db_Select('private_msg', 'pm_id', $qry)) {
                while ($row = $db2->db_Fetch(MYSQL_ASSOC)) {
                    if ($pmHandler->del($row['pm_id']) !== FALSE) {
                        $cnt++;
                    }
                }
            }
            $start = max($start + 1, time());
            $results[E_MESSAGE_SUCCESS][$start] = str_replace('--COUNT--', $cnt, ADLAN_PM_73);
        } else {
            $start = max($start + 1, time());
            $results[E_MESSAGE_ERROR][$start] = ADLAN_PM_72;
        }
    }
    if (isset($opts['attach'])) {
        // Check for orphaned and missing attachments
        require_once e_HANDLER . 'file_class.php';
        $fl = new e_file();
        $missing = array();
        $orphans = array();
        $fileArray = $fl->get_files(e_PLUGIN . 'pm/attachments');
        if ($db2->select('private_msg', 'pm_id, pm_attachments', "pm_attachments != ''")) {
            while ($row = $db2->fetch(MYSQL_ASSOC)) {
                $attachList = explode(chr(0), $row['pm_attachments']);
                foreach ($attachList as $a) {
                    $found = FALSE;
                    foreach ($fileArray as $k => $fd) {
                        if ($fd['fname'] == $a) {
                            $found = TRUE;
                            unset($fileArray[$k]);
                            break;
                        }
                    }
                    if (!$found) {
                        $missing[] = $row['pm_id'] . ':' . $a;
                    }
                }
            }
        }
        // Any files left in $fileArray now are unused
        if (count($fileArray)) {
            foreach ($fileArray as $k => $fd) {
                unlink($fd['path'] . $fd['fname']);
                $orphans[] = $fd['fname'];
            }
        }
        $attachMessage = str_replace(array('--ORPHANS--', '--MISSING--'), array(count($orphans), count($missing)), ADLAN_PM_79);
        if (TRUE) {
            // Mostly for testing - probably disable this
            if (count($orphans)) {
                $attachMessage .= '[!br!]Orphans:[!br!]' . implode('[!br!]', $orphans);
            }
            if (count($missing)) {
                $attachMessage .= '[!br!]Missing:[!br!]' . implode('[!br!]', $missing);
            }
        }
        $start = max($start + 1, time());
        $results[E_MESSAGE_SUCCESS][$start] = $attachMessage;
    }
    $e107->admin_log->logArrayAll('PM_ADM_03', makeLogEntry($results));
    foreach ($results as $k => $r) {
        foreach ($r as $sk => $s) {
            $results[$k][$sk] = str_replace('[!br!]', '<br />', $s);
        }
    }
    return $results;
}