📄 devmode_functions.php
字号:
/*****************************************************
function dev_install_mail_templates()
-----DESCRIPTION: -----------------------------------
Read all mail templates in from files, parse them, and
update the database.
-----ARGUMENTS: -------------------------------------
location Directory to import from
-----RETURNS: ---------------------------------------
Nothing.
*****************************************************/
function dev_install_mail_templates($location) {
global $db, $_REQUEST;
if (!file_exists($location)) {
$errors[] .= "[ERROR] $location doesn't exist; cannot import e-mail templates: $php_errormsg\n";
}
if (!@is_dir($location)) {
$errors[] .= "[ERROR] $location isn't a directory or can't be stat()'d; cannot import e-mail templates: $php_errormsg\n";
}
$db->query("DELETE FROM template_email");
$languages = array();
$db->query("SELECT installid AS id, name FROM languages WHERE !custom");
if (!$db->num_rows()) {
$errors .= "No languages are defined, or none were selected. Cannot import.";
return $errors;
}
while ($res = $db->row_array()) {
$languages[$res['id']]['name'] = $res['name'];
$languages[$res['id']]['dir'] = $location.'/'.$res['name'];
}
$languages[-1]['name'] = 'Tech bodies';
$languages[-1]['dir'] = $location;
foreach ($languages AS $lang_id => $lang_data) {
if (!@file_exists($languages[$lang_id]['dir'])) {
$errors .= "[ERROR] Directory " . $languages[$lang_id]['dir'] . " doesn't exist: $php_errormsg\n";
continue;
}
if (!@is_dir($languages[$lang_id]['dir'])) {
// It's a plain file, not a directory. Can't import like this.
$errors .= "[ERROR] " . $languages[$lang_id]['dir'] . " exists but isn't a directory or can't be stat()'d: $php_errormsg\n";
continue;
}
if (!$dirhandle = opendir($languages[$lang_id]['dir'])) {
$errors .= "[ERROR] " . $languages[$lang_id]['dir'] . " exists but can't be opened for reading: $php_errormsg\n";
continue;
} else {
$errors .= "Opening " . $languages[$lang_id]['dir'] . "\n";
while (false !== ($file = readdir($dirhandle))) {
$template = basename($file, '.txt');
$file = $languages[$lang_id]['dir'].'/'.$file;
if (($file != '.') AND ($file != '..') AND is_file($file)) {
if ($handle = @fopen($file, 'rb')) {
$data = array();
while (!feof($handle)) {
$data[] = fgets($handle, 4096);
}
if (preg_match("/^Subject:(.*)$/i", $data[0], $matches)) {
$subject = mysql_escape_string(trim($matches[1]));
array_shift($data);
} else {
$subject = '';
}
if (preg_match("/^Description:(.*)$/i", $data[0], $matches)) {
$description = mysql_escape_string(trim($matches[1]));
array_shift($data);
} else {
unset($description);
}
$data = join('', $data);
$parsed = parse_conditionals($data);
if (stristr($file, 'TECHBODY')) {
$import_cat = 'Tech Emails';
} else{
$import_cat = 'User Emails';
}
$thelanguage = $lang_id;
if ($thelanguage == '-1') {
$thelanguage = '0';
}
$db->query("
INSERT INTO template_email SET
description = '$description',
template = '" . mysql_escape_string($parsed) . "',
template_unparsed = '" . mysql_escape_string($data) . "',
name = '" . mysql_escape_string($template) . "',
language = '$thelanguage',
category = '$import_cat',
subject = '$subject'
");
} else {
$imported[$template][$lang_id] = 0;
$errors .= "Can't open $file: $php_errormsg\n";
}
}
}
closedir($dirhandle);
}
}
// now give the descriptions to all the languages that don't have a description
$db->query("SELECT name, description FROM template_email WHERE description != ''");
while ($result = $db->row_array()) {
$template_data[$result[name]] = $result[description];
}
foreach ($template_data AS $key => $var) {
$db->query("UPDATE template_email SET description = '" . addslashes($var) . "' WHERE name = '" . addslashes($key) . "'");
}
return $errors;
}
/*****************************************************
function install_words_import()
-----DESCRIPTION: -----------------------------------
Imports all language translations.
-----ARGUMENTS: -------------------------------------
location Directory to import from
-----RETURNS: ---------------------------------------
Nothing.
*****************************************************/
function dev_install_words_import($location) {
global $db;
if (!file_exists($location)) {
$errors .= "[ERROR] $location doesn't exist; cannot import templates: $php_errormsg\n";
}
if (!@is_dir($location)) {
$errors .= "[ERROR] $location isn't a directory or can't be stat()'d; cannot import templates: $php_errormsg\n";
}
$db->query("DELETE FROM template_words");
$catnames = $db->query_return_array_id("SELECT id, name FROM template_words_cat", 'name');
$catnames[0] = '';
$languages = array();
$db->query("SELECT installid AS id, name FROM languages WHERE !custom");
if (!$db->num_rows()) {
$errors .= "No languages are defined, or none were selected. Cannot export.";
return $errors;
}
while ($res = $db->row_array()) {
$languages[$res['id']]['name'] = $res['name'];
$languages[$res['id']]['file'] = $location . '/' . str_replace('/', '_', $res['name']);
}
foreach ($languages AS $langid => $lang) {
if (!$handle = fopen($lang['file'], 'rb')) {
$errors .= "Cannot open " . $langid['file'] . "for reading $php_errormsg\n";
return $errors;
}
$data = array();
while (!feof($handle)) {
$data[] = fgets($handle, 4096);
}
@fclose($handle);
$count = 0;
$catid = 0;
foreach ($data as $line) {
$count++;
$matches = array();
$line = trim($line);
if (!$line or (preg_match('/^#/', $line))) {
continue;
}
if (preg_match("/^%%(.*)/", $line, $cat)) {
$catname = $cat[1];
$catid = array_search($catname, $catnames);
if (!$catid) {
$db->query("INSERT INTO template_words_cat (name) VALUES ('" . mysql_escape_string($catname) . "')");
$catid = $db->last_id();
$errors .= "Category $catname not found, created new category, ID #$catid\n";
$catnames = $db->query_return_array_id("SELECT id, name FROM template_words_cat", 'name');
}
continue;
}
if (preg_match("/^([^:]*):(.*)$/", $line, $matches)) {
$matches[1] = mysql_escape_string(trim($matches[1]));
$matches[2] = mysql_escape_string(trim($matches[2]));
if ($imported[$matches[1]][$langid] == 1) {
$errors .= "Duplicate wordref ($matches[1]) for language ". $languages[$langid]['name'] . "\n";
} else {
$db->query("
INSERT INTO template_words (wordref, category, text, language)
VALUES ('$matches[1]', '$catid', '$matches[2]', '$langid')
");
$imported[$matches[1]][$langid] = 1;
}
} else {
$errors .= "Error in file " . $languages[$langid]['file'] . " on line $count.\n";
}
}
}
foreach ($imported AS $wordref => $val) {
foreach ($languages AS $lang_id => $lang) {
if ($val[$lang_id] != 1) {
$missing_words[$lang_id] .= "\t - $wordref\n";
}
}
$rows[] = $row;
}
foreach ($missing_words AS $key => $var) {
$errors .= "Missing words for " . $languages[$key]['name'] . "\n$var";
}
return $errors;
}
// Load 250,000 tickets into the database.
// Do not do this unless you really, really mean it.
function load_fake_tickets($location, $ticket_count = 0) {
global $db;
// Load some fragments.
if (!is_dir($location)) {
mistake("$location isn't a directory. Nothing to load.");
}
if (!($dir = @opendir($location))) {
mistake("Can't open $location for reading.");
}
$fragments = array();
while ($file = @readdir($dir)) {
if ($file == '.' OR $file == '..' OR (substr($file, 0, 1) == '.') OR !is_file("$location/$file")) {
continue;
}
if ($handle = @fopen($location . "/$file", 'rb')) {
$data = @fread($handle, filesize("$location/$file"));
@fclose($handle);
$data = explode("\n\n", $data);
$fragments = array_merge($fragments, $data);
}
}
$tech_ids = $db->query_return_array_id("SELECT id FROM tech WHERE !disabled", 'id');
$user_ids = $db->query_return_array_id("SELECT id FROM user WHERE !disabled", 'id');
$cat_ids = $db->query_return_array_id("SELECT id FROM ticket_cat", 'id');
$pri_ids = $db->query_return_array_id("SELECT id FROM ticket_pri", 'id');
$startdate = strtotime(date('Y-m-d') . ' - 1 weeks');
while ($ticket_count < 250000) {
$ticket_count++;
$user_msgs = rand(1,3);
$tech_msgs = rand(1,3);
$minutes = rand(1,100);
$subject = "Test ticket #$ticket_count";
$techid = array_rand($tech_ids);
$userid = array_rand($user_ids);
$pri = array_rand($pri_ids);
$cat = array_rand($cat_ids);
$this_start = $startdate;
$awaiting_tech = (rand(1,2) - 1);
$ref = make_ticket_ref();
// Create the ticket
$db->query("INSERT INTO ticket SET
subject = '$subject',
userid = '$userid',
tech = '$techid',
category = '$cat',
priority = '$pri',
language = '1',
is_open = '1',
awaiting_tech = '$awaiting_tech',
date_opened = '$this_start',
ref = '$ref'");
$id = $db->last_id();
unset($messages);
for ($i = 1; $i <= $user_msgs; $i++) {
$message['type'] = 'user';
$this_start = strtotime(date('Y-m-d') . " +$minutes minutes");
$message['date'] = $this_start;
$message['message'] = mysql_escape_string(array_rand($fragments));
$messages[] = $message;
}
for ($i = 1; $i <= $tech_msgs; $i++) {
$message['type'] = 'user';
$this_start = strtotime(date('Y-m-d') . " +$minutes minutes");
$message['date'] = $this_start;
$message['message'] = mysql_escape_string($fragments[array_rand($fragments)]);
$messages[] = $message;
}
shuffle($messages);
foreach($messages AS $message) {
if ($message['type'] == 'user') {
$uid = $userid;
$tid = 0;
$userdate = $message['date'];
} else {
$uid = 0;
$tid = $techid;
$techdate = $message['date'];
}
$db->query("INSERT INTO ticket_message SET
ticketid = '$id',
message = '$message[message]',
date = '$message[date]',
techid = '$techid',
userid = '$userid'");
}
$db->query("UPDATE ticket SET
date_lastreply = '$userdate',
date_lastreply_tech = '$techdate'");
if (!($ticket_count % 20)) {
echo "Created $ticket_count tickets so far...<br />";
}
if (!($ticket_count % 1000)) {
break;
}
}
return $ticket_count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -