devmode_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:45k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: devmode_functions.php,v $
- // | $Date: 2004/02/13 02:12:31 $
- // | $Revision: 1.29 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Utility functions for the administration interface
- // | developer mode tools
- // +-------------------------------------------------------------+
- error_reporting(E_ALL ^ E_NOTICE);
- /*****************************************************
- function template_reparse()
- -----DESCRIPTION: -----------------------------------
- Re-parse all templates stored in the DB
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function template_reparse() {
- global $db;
- $db->query("SELECT * FROM template");
- while ($result = $db->row_array()) {
- $template = parse_conditionals($result[template_unparsed]);
- $db2->query("UPDATE template SET template = '" . mysql_escape_string($template) . "' WHERE id = '$result[id]'");
- }
- }
- /*****************************************************
- function dev_import_templates()
- -----DESCRIPTION: -----------------------------------
- Read all templates in from files, parse them, and
- update the database.
- -----ARGUMENTS: -------------------------------------
- location Directory to import from
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_import_templates($location) {
- global $db;
- if (!file_exists($location)) {
- mistake("[ERROR] $location doesn't exist; cannot importn");
- }
- if (!is_dir($location)) {
- mistake("[ERROR] $location isn't a directory; cannot importn");
- }
- if (!($dir = @opendir($location))) {
- mistake("[ERROR] Can't open $location for reading; cannot importn");
- }
- while ($file = @readdir($dir)) {
- if ($file == '.' OR $file == '..' OR (substr($file, 0, 1) == '.') OR !is_file("$location/$file")) {
- continue;
- }
-
- // read file
- if ($handle = @fopen($location . "/$file", 'rb')) {
- $template = @fread($handle, filesize($location . "/$file"));
- $file = str_replace('.html', '', $file);
- @fclose($handle);
- $parsed_template = parse_conditionals($template);
- $db->query("SELECT * FROM template WHERE name = '" . mysql_escape_string($file) . "'");
- if ($db->num_rows()) {
- $db->query("
- UPDATE template SET
- template = '" . mysql_escape_string($parsed_template) . "',
- template_unparsed = '" . mysql_escape_string($template) . "'
- WHERE name = '" . mysql_escape_string($file) . "'
- ");
- $return .= "[NOTICE] Imported $file.n";
- } else {
- $db->query("INSERT INTO template SET
- template = '" . mysql_escape_string($parsed_template) . "',
- template_unparsed = '" . mysql_escape_string($template) . "',
- name = '" . mysql_escape_string($file) . "',
- category = '0',
- description = '',
- upgraded = '0',
- changed = '0',
- custom = '0',
- version_upgrade = '0',
- displayorder = '9999',
- backup = '0'
- ");
- $return .= "[NOTICE] Added new template $file.n";
- }
- } else {
- $return .= "[WARN] Couldn't open file "$file" for reading.n";
- }
- }
- return $return;
- }
- /*****************************************************
- function dev_import_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_import_mail_templates($location) {
- global $db, $_REQUEST;
- if (!file_exists($location)) {
- $errors[] = "[ERROR] $location doesn't exist; cannot import e-mail templates: $php_errormsg";
- }
- if (!@is_dir($location)) {
- $errors[] = "[ERROR] $location isn't a directory or can't be stat()'d; cannot import e-mail templates: $php_errormsg";
- }
- $orig_langs = $_REQUEST['languages'];
- if (in_array('tech', $_REQUEST['languages']) OR !$_REQUEST['languages']) {
- $get_tech = 1;
- if (count($_REQUEST['languages'])) {
- foreach($_REQUEST['languages'] AS $val) {
- if ($val != 'tech') {
- $langs[] = $val;
- }
- $_REQUEST['languages'] = $langs;
- }
- $get_norm = 1;
- } else {
- if (!$_REQUEST['languages']) {
- $get_norm = 1;
- } else {
- $_REQUEST['languages'] = NULL;
- }
- }
- } else {
- $get_norm = 1;
- }
- if (!count($errors)) {
- if ($_REQUEST['languages']) {
- $langs = ' where ID in ' . array2sql($_REQUEST['languages']);
- }
- $db->query("SELECT id, name FROM languages $langs");
- if (!$db->num_rows() AND !$get_tech) {
- $errors[] = "No languages are defined. Cannot import.";
- }
- 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[] = "Directory " . $languages[$lang_id]['dir'] . " doesn't exist: $php_errormsg";
- continue;
- }
-
- if (!@is_dir($languages[$lang_id]['dir'])) {
- // It's a plain file, not a directory. Can't import like this.
- $errors[] = $languages[$lang_id]['dir'] . " exists but isn't a directory or can't be stat()'d: $php_errormsg";
- continue;
- }
- if (!$dirhandle = opendir($languages[$lang_id]['dir'])) {
- $errors[] = $languages[$lang_id]['dir'] . " exists but can't be opened for reading: $php_errormsg";
- continue;
- } else {
- while (false !== ($file = readdir($dirhandle))) {
- $template = basename($file, '.txt');
- $file = $languages[$lang_id]['dir'].'/'.$file;
- if (($file != '.') AND ($file != '..') AND is_file($file)) {
- if ((stristr($file, 'TECHBODY') AND $get_tech) OR (!stristr($file, 'TECHBODY') AND !$get_tech) OR (!stristr($file, 'TECHBODY') AND $get_norm)) {
- if ($handle = @fopen($file, 'rb')) {
- $data = array();
- while (!feof($handle)) {
- $data[] = fgets($handle, 4096);
- }
- if (preg_match("/^Subject:(.*)$/i", $data[0], $matches)) {
- $subject = ", subject = '" . mysql_escape_string(trim($matches[1])) . "'";
- array_shift($data);
- } else {
- $subject = '';
- }
-
- $data = join('', $data);
- $parsed = parse_conditionals($data);
- $db->query("SELECT id FROM template_email WHERE
- name = '" . mysql_escape_string($template) . "'
- AND language = $lang_id
- AND !backup");
- if ($db->num_rows()) {
- $db->query("UPDATE template_email
- SET template = '" . mysql_escape_string($parsed) . "',
- template_unparsed = '" . mysql_escape_string($data) . "'
- $subject
- WHERE language = $lang_id
- AND name = '" . mysql_escape_string($template) . "'
- AND !backup
- ");
- if ($db->affected_rows()) {
- $imported[$template][$lang_id] = -2;
- } else {
- $imported[$template][$lang_id] = -1;
- }
- } else {
- $imported[$template][$lang_id] = -2;
- if (stristr($file, 'TECHBODY')) {
- $import_cat = 'Tech Emails';
- } else{
- $import_cat = 'User Emails';
- }
- $db->query("INSERT INTO template_email SET
- template = '" . mysql_escape_string($parsed) . "',
- template_unparsed = '" . mysql_escape_string($data) . "',
- name = '" . mysql_escape_string($template) . "',
- language = '$lang_id',
- category = '$import_cat',
- description = '',
- upgraded = '0',
- changed = '0',
- custom = '1',
- version_upgrade = '0',
- displayorder = '9999',
- backup = '0'
- $subject
- ");
- $db->query("INSERT INTO template_email SET
- template = '" . mysql_escape_string($parsed) . "',
- template_unparsed = '" . mysql_escape_string($data) . "',
- name = '" . mysql_escape_string($template) . "',
- language = '$lang_id',
- category = '$import_cat',
- description = '',
- upgraded = '0',
- changed = '0',
- custom = '1',
- version_upgrade = '0',
- displayorder = '9999',
- backup = '1'
- $subject
- ");
- }
- } else {
- $imported[$template][$lang_id] = 0;
- $errors[] = "Can't open $file: $php_errormsg";
- }
- }
- }
- }
- closedir($dirhandle);
- }
- }
-
- $cols = array('Template Name');
- foreach ($languages as $lang) {
- $cols[] = $lang['name'];
- }
- foreach ($imported AS $template => $val) {
- $row = array($template);
- foreach ($languages AS $lang_id => $lang) {
- if ($val[$lang_id] == -2) {
- $result = '<FONT COLOR="green">Imported</FONT>';
- } elseif ($val[$lang_id] == -1) {
- $result = '<FONT COLOR="green">No Change</FONT>';
- } elseif ($val[$lang_id] === FALSE) {
- $result = '<FONT COLOR="red">Error</FONT>';
- } else {
- $result = 'Not defined';
- }
- $row[] = "<CENTER>$result</CENTER>";
- if (is_string($val[$lang_id])) {
- $errors[] = $val[$lang_id];
- }
- }
- $rows[] = $row;
- }
- }
- table_header('Import Results');
- table_content($cols, $rows);
- table_footer();
- if (count($errors)) {
- table_header('Import Errors');
- table_content(NULL, $errors);
- table_footer();
- }
- }
- /*****************************************************
- function dev_remove_edit()
- -----DESCRIPTION: -----------------------------------
- Remove the "changed" and "upgraded" status flags on
- all templates.
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_remove_edit() {
- global $db;
- $db->query('UPDATE template SET changed = 0, upgraded = 0, version_upgrade = 0');
- }
- /*****************************************************
- function dev_make_default()
- -----DESCRIPTION: -----------------------------------
- Make all templates "stock" templates (remove their
- custom flag)
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_make_default() {
- global $db;
- $db->query("UPDATE template SET custom = '0'");
- }
- /*****************************************************
- function dev_undo_changes()
- -----DESCRIPTION: -----------------------------------
- Clear all templates' changed and upgraded flags.
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_undo_changes() {
- global $db;
- $db->query("UPDATE template SET changed = '0', upgraded = '0'");
- }
- /*****************************************************
- function dev_export_templates()
- -----DESCRIPTION: -----------------------------------
- Export HTML templates to files in the specified
- directory.
- -----ARGUMENTS: -------------------------------------
- location Directory to place templates into
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_export_templates($location) {
- global $db, $_REQUEST;
- if (!file_exists($location)) {
- mistake("[ERROR] $location doesn't exist; cannot export HTML templates");
- }
- if (!is_dir($location)) {
- mistake("[ERROR] $location isn't a directory; cannot export HTML templates");
- }
- if ($_REQUEST['id']) {
- $db->query("SELECT * FROM template WHERE !backup AND id = '$_REQUEST[id]'");
- } elseif ($_REQUEST['search']) {
- $db->query("SELECT * FROM template WHERE !backup AND template.name LIKE '%" . mysql_escape_string($_REQUEST['search']) . "%'");
- } else {
- $db->query("SELECT * FROM template WHERE !backup");
- }
- if (!$db->num_rows()) {
- error('No templates exported');
- }
- while ($data = $db->row_array()) {
- if ($handle = @fopen($location . '/' . $data['name'] . '.html', 'wb')) {
- @fwrite($handle, $data['template_unparsed']);
- @fclose($handle);
- echo "[NOTICE] Exported $data[name].html<br />";
- } else {
- echo "[WARN] Couldn't open $location/$data[name] for writing.<br />";
- }
- }
- }
- /*****************************************************
- function dev_export_mail_templates()
- -----DESCRIPTION: -----------------------------------
- Export e-mail templates to files in the specified
- directory.
- -----ARGUMENTS: -------------------------------------
- location Directory to place templates into
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_export_mail_templates($location) {
- global $db, $_REQUEST;
- umask(0);
- $orig_langs = $_REQUEST['languages'];
- if (in_array('tech', $_REQUEST['languages'])) {
- $get_tech = 1;
- if (count($_REQUEST['languages']) > 1) {
- foreach($_REQUEST['languages'] AS $val) {
- if ($val != 'tech') {
- $langs[] = $val;
- }
- $_REQUEST['languages'] = $langs;
- }
- $get_norm = 1;
- } else {
- $_REQUEST['languages'] = NULL;
- }
- } else {
- $get_norm = 1;
- }
- if (!file_exists($location)) {
- // Try to create the directory if it doesn't exist
- if (!@mkdir($location, '0775')) {
- // We couldn't create the directory, so we can't export this language
- $errors[] = "[ERROR] $location doesn't exist and cannot be created: $php_errormsg";
- } else {
- $errors[] = "[NOTICE] Created directory $location<BR />n";
- }
- }
- if (!@is_dir($location)) {
- $errors[] = "[ERROR] $location isn't a directory or can't be stat()'d; cannot export e-mail templates: $php_errormsg";
- }
- // Fetch languages to process
- if (!count($errors)) {
- if ($_REQUEST['languages']) {
- $langs = ' where ID in ' . array2sql($_REQUEST['languages']);
- }
- $db->query("SELECT id, name FROM languages $langs");
- if (!$db->num_rows()) {
- $errors[] = "No languages are defined, or none were selected. Cannot export.";
- }
- while ($res = $db->row_array()) {
- $languages[$res['id']]['name'] = $res['name'];
- $languages[$res['id']]['dir'] = $location.'/'.$res['name'];
- if (!@file_exists($languages[$res['id']]['dir'])) {
- // First try to create the directory
- if (!@mkdir($languages[$res['id']]['dir'], '0775')) {
- $errors[] = "Directory " . $languages[$res['id']]['dir'] . " doesn't exist and couldn't be created: $php_errormsg";
- } else {
- $errors[] = "[NOTICE] Created directory " . $languages[$res['id']]['dir'];
- }
- }
-
- if (!@is_dir($languages[$res['id']]['dir'])) {
- // It's a plain file, not a directory. Can't export like this.
- $errors[] = $languages[$res['id']]['dir'] . " exists but isn't a directory or can't be stat()'d: $php_errormsg";
- }
- }
- // Process each language
-
- $db->query("SELECT template_unparsed, subject, name, language FROM template_email WHERE language AND !backup ORDER BY language");
- if (!$db->num_rows()) {
- mistake("No e-mail templates are defined. Cannot export.");
- }
- while ($res = $db->row_array()) {
- if (stristr($res['name'], 'TECHBODY')) {
- if (!$get_tech) {
- continue;
- }
- $name = "$location/$res[name].txt";
- } else {
- if (!$get_norm) {
- continue;
- }
- $name = $languages[$res['language']]['dir'] . '/' . $res['name'] . '.txt';
- }
- if ($handle = @fopen($name, 'wb')) {
- @fwrite($handle, "Subject: $res[subject]n");
- @fwrite($handle, $res['template_unparsed']);
- @fclose($handle);
- if(stristr($res['name'], 'TECHBODY')) {
- $exported[$res['name']][-1] = -1;
- } else {
- $exported[$res['name']][$res['language']] = -1;
- }
- } else {
- $exported[$res['name']][$res['language']] = "[WARNING] Couldn't export $res[name] to $name: $php_errormsg";
- }
- }
- $cols = array('Template Name');
- $languages['-1']['name'] = "Tech Mails";
- foreach ($languages as $lang_id => $lang) {
- if (in_array($lang_id, $orig_langs)) {
- $cols[] = $lang['name'];
- }
- }
-
- foreach ($exported AS $template => $val) {
- $row = array($template);
- foreach ($languages AS $lang_id => $lang) {
- if (in_array($lang_id, $orig_langs)) {
- if(stristr($template, 'TECHBODY')) {
- if ($val[-1] == -1) {
- $result = '<FONT COLOR="green">Tech E-mail Template Exported</FONT>';
- } elseif ($val[-1]) {
- $result = '<FONT COLOR="red">Error</FONT>';
- }
- } else {
- if ($val[$lang_id] == -1) {
- $result = '<FONT COLOR="green">Exported</FONT>';
- } elseif ($val[$lang_id]) {
- $result = '<FONT COLOR="red">Error</FONT>';
- } else {
- $result = 'Not Defined';
- }
- }
- $row[] = "<CENTER>$result</CENTER>";
- if (is_string($val[$lang_id])) {
- $errors[] = $val[$lang_id];
- }
- if(stristr($template, 'TECHBODY')) {
- break;
- }
- }
- }
- $rows[] = $row;
- }
- }
- table_header('Export Results');
- table_content($cols, $rows);
- table_footer();
- if (count($errors)) {
- table_header('Export Errors');
- table_content(NULL, $errors);
- table_footer();
- }
- }
- /*****************************************************
- function dev_reset_backup_templates()
- -----DESCRIPTION: -----------------------------------
- Recreate backups from current templates for all
- templates
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_reset_backup_templates() {
- global $db;
- $db->query("DELETE FROM template WHERE backup");
- $data = $db->query_return_array("SELECT * FROM template");
- if (@is_array($data)) {
- foreach($data AS $val) {
- $db->query('INSERT INTO template
- (name, template, category, description, upgraded, changed, custom, version_upgrade, template_unparsed, displayorder, backup) VALUES (
- ''.mysql_escape_string($val[name]).'',
- ''.mysql_escape_string($val[template]).'',
- ''.mysql_escape_string($val[category]).'',
- ''.mysql_escape_string($val[description]).'',
- '0',
- '0',
- '0',
- '0',
- ''.mysql_escape_string($val[template_unparsed]).'',
- ''.mysql_escape_string($val[displayorder]).'',
- '1'
- )');
- }
- }
- }
- /*****************************************************
- function dev_reset_backups()
- -----DESCRIPTION: -----------------------------------
- Makes copies of all current non-backup templates
- as the system's backup templates, replacing
- backup templates of the same name if they already
- existed.
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_reset_backups() {
- global $db;
- $db->query('SELECT * FROM template_email');
- while($res = $db->row_array()) {
- if ($res['backup']) {
- $ids[] = $res['name'];
- } else {
- $data[] = $res;
- }
- }
- if (count($ids)) {
- $db->query('DELETE FROM template_email WHERE backup AND name IN ' . array2sql($ids));
- }
- if (@is_array($data)) {
- foreach($data AS $val) {
- $db->query('INSERT INTO template_email
- (name, template, category, description, upgraded, changed, custom, version_upgrade, template_unparsed, displayorder, backup, language, subject) VALUES (
- ''.mysql_escape_string($val[name]).'',
- ''.mysql_escape_string($val[template]).'',
- ''.mysql_escape_string($val[category]).'',
- ''.mysql_escape_string($val[description]).'',
- '0',
- '0',
- '0',
- '0',
- ''.mysql_escape_string($val[template_unparsed]).'',
- ''.mysql_escape_string($val[displayorder]).'',
- 1,
- ''.mysql_escape_string($val[language]).'',
- ''.mysql_escape_string($val[subject]).''
- )');
- }
- }
- }
- /*****************************************************
- function dev_restore_norm_templates()
- -----DESCRIPTION: -----------------------------------
- Makes copies of all current backup templates
- and makes non-backup templates from them.
- Destroys non-backup templates.
- -----ARGUMENTS: -------------------------------------
- None
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_restore_norm_templates() {
- global $db;
- $templates = $db->query_return_array('SELECT * FROM template WHERE backup');
- $db->query("DELETE FROM template WHERE !backup");
- if (@is_array($templates)) {
- foreach($templates AS $val) {
- $db->query('INSERT INTO template
- (name, template, category, description, upgraded, changed, custom, version_upgrade, template_unparsed, displayorder, backup) VALUES (
- ''.mysql_escape_string($val[name]).'',
- ''.mysql_escape_string($val[template]).'',
- ''.mysql_escape_string($val[category]).'',
- ''.mysql_escape_string($val[description]).'',
- '0',
- '0',
- '0',
- '0',
- ''.mysql_escape_string($val[template_unparsed]).'',
- ''.mysql_escape_string($val[displayorder]).'',
- '0'
- )');
- }
- }
- $templates = $db->query_return_array('SELECT * FROM template_email WHERE backup');
- $db->query("DELETE FROM template_email WHERE !backup");
- if (@is_array($templates)) {
- foreach($templates AS $val) {
- $db->query('INSERT INTO template_email
- (name, template, category, description, upgraded, changed, custom, version_upgrade, template_unparsed, displayorder, backup, language, subject) VALUES (
- ''.mysql_escape_string($val[name]).'',
- ''.mysql_escape_string($val[template]).'',
- ''.mysql_escape_string($val[category]).'',
- ''.mysql_escape_string($val[description]).'',
- '0',
- '0',
- '0',
- '0',
- ''.mysql_escape_string($val[template_unparsed]).'',
- ''.mysql_escape_string($val[displayorder]).'',
- '0',
- ''.mysql_escape_string($val[language]).'',
- ''.mysql_escape_string($val[subject]).''
- )');
- }
- }
- }
- /*****************************************************
- function dev_words_export()
- -----DESCRIPTION: -----------------------------------
- Exports all language translations.
- -----ARGUMENTS: -------------------------------------
- location Directory to export to
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_words_export($location) {
- global $db;
- umask(0);
- if (!file_exists($location)) {
- // Try to create the directory if it doesn't exist
- if (!@mkdir($location, '0775')) {
- // We couldn't create the directory, so we can't export this language
- $errors[] = "[ERROR] $location doesn't exist and cannot be created: $php_errormsg";
- } else {
- $errors[] = "[NOTICE] Created directory $location<BR />n";
- }
- }
- if (!@is_dir($location)) {
- $errors[] = "[ERROR] $location isn't a directory or can't be stat()'d; cannot export template phrases: $php_errormsg";
- }
- // Fetch languages to process
- if (!count($errors)) {
- $catnames = $db->query_return_array_id("SELECT id, name FROM template_words_cat", 'name');
- if ($_REQUEST['languages']) {
- $db->query("SELECT id, name FROM languages WHERE id IN " . array2sql($_REQUEST['languages']));
- if (!$db->num_rows()) {
- $errors[] = "No languages were selected. Cannot export.";
- }
- } else {
- $db->query("SELECT id, name FROM languages");
- if (!$db->num_rows()) {
- $errors[] = "No languages are defined. Cannot export.";
- }
- }
- $languages = array();
- $legend = "# Format:n#n# Empty lines, and lines beginning with "#" are comments. Lines beginning withn# "%%" specify a new (or continuing) category. Empty lines are ignored asn# comments. When a category is specified, every wordref beneath it is added ton# that category. The template file should begin with a category specification,n# otherwise, wordrefs appearing prior to a category specifier will be assignedn# no category.n#n# All other lines are expected to be in the format: n# wordref: full phrase textn#n# To delete a wordref, specify it on a line by itself with no phrase n# definition, as shown: n# wordref:nn";
- if (!$_REQUEST['languages']) {
- echo "No languages selected";
- return;
- }
- while ($res = $db->row_array()) {
- if (!$res['id'] AND !@in_array('0', $_REQUEST['languages'])) {
- continue;
- }
- $languages[$res['id']]['name'] = $res['name'];
- $languages[$res['id']]['file'] = $location . '/' . str_replace('/', '_', $res['name']);
- $languages[$res['id']]['prev'] = 0;
- if (!$languages[$res['id']]['handle'] = @fopen($languages[$res['id']]['file'], 'wb')) {
- $errors[] = "Cannot open " . $languages[$res['id']]['file'] . "for writing $php_errormsg";
- }
- if (!@fwrite($languages[$res['id']]['handle'], $legend)) {
- $errors[] = "Couldn't write to " . $languages[$res['id']]['file'] . " for header: $php_errormsg";
- }
- }
- // Process data
- $data = $db->query_return_array("SELECT * FROM template_words ORDER BY language, category, wordref");
- if (!$db->num_rows()) {
- mistake("No template phrases are defined. Cannot export.");
- }
-
- foreach($data AS $val) {
- if ($val['category'] != $languages[$val['language']]['prev']) {
- $languages[$val['language']]['prev'] = $val['category'];
- $line = "n%%{$catnames[$val['category']]}nn";
- if (!@fwrite($languages[$val['language']]['handle'], $line)) {
- $errors[] = "Couldn't write to " . $languages[$val['language']]['file'] . " for entry $val[wordref]: $php_errormsg";
- }
- }
- $line = "$val[wordref]: $val[text]n";
- if (!@fwrite($languages[$val['language']]['handle'], $line)) {
- $errors[] = "Couldn't write to " . $languages[$val['language']]['file'] . " for entry $val[wordref]: $php_errormsg";
- $exported[$val['wordref']][$val['language']] = 1;
- }
- $exported[$val['wordref']][$val['language']] = -1;
- }
- $cols = array('Template Name');
- foreach ($languages as $lang) {
- $cols[] = $lang['name'];
- @fclose($lang['handle']);
- }
- foreach ($exported AS $template => $val) {
- $row = array($template);
- foreach ($languages AS $lang_id => $lang) {
- if ($val[$lang_id] == -1) {
- $result = '<FONT COLOR="green">Exported</FONT>';
- } elseif ($val[$lang_id] > 0) {
- $result = '<FONT COLOR="red">Error</FONT>';
- } else {
- $result = 'Not Defined';
- }
- $row[] = "<CENTER>$result</CENTER>";
- if (is_string($val[$lang_id])) {
- $errors[] = $val[$lang_id];
- }
- }
- $rows[] = $row;
- }
- }
- table_header('Export Results');
- table_content($cols, $rows);
- table_footer();
- if (count($errors)) {
- table_header('Export Errors');
- table_content(NULL, $errors);
- table_footer();
- }
- }
- /*****************************************************
- function dev_words_import()
- -----DESCRIPTION: -----------------------------------
- Imports all language translations.
- -----ARGUMENTS: -------------------------------------
- location Directory to import from
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_words_import($location) {
- global $db, $_REQUEST;
-
- if (!file_exists($location)) {
- $errors[] = "[ERROR] $location doesn't exist; cannot import templates: $php_errormsg";
- }
- if (!@is_dir($location)) {
- $errors[] = "[ERROR] $location isn't a directory or can't be stat()'d; cannot import templates: $php_errormsg";
- }
- if (!count($errors)) {
- if ($_REQUEST['dryrun']) {
- $errors[] = "[NOTICE] Dry run; no changes were made.";
- }
-
- $catnames = $db->query_return_array_id("SELECT id, name FROM template_words_cat", 'name');
- $catnames[0] = '';
- $languages = array();
- if (@in_array('0', $_REQUEST['languages']) OR !$_REQUEST['languages']) {
- $languages[0] = array(
- 'name' => 'Default',
- 'file' => "$location/Default",
- );
- }
- if ($_REQUEST['languages']) {
- $langs = ' WHERE id IN ' . array2sql($_REQUEST['languages']);
- }
- $db->query("SELECT id, name FROM languages $langs");
- if (!$db->num_rows()) {
- $errors[] = "No languages are defined, or none were selected. Cannot export.";
- } else {
- 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 (!$_REQUEST['dryrun']) {
- if ($_REQUEST['replace_all'] AND $_REQUEST['replace_all_confirm']) {
- $db->query("DELETE FROM template_words WHERE language = '$langid'");
- $errors[] = "[NOTICE] Purged all existing words for the $lang[name] language.";
- }
- } else {
- if ($_REQUEST['replace_all'] AND $_REQUEST['replace_all_confirm']) {
- $errors[] = "[NOTICE] Would delete all existing words for the $lang[name] language.";
- }
- }
- if (!$handle = fopen($lang['file'], 'rb')) {
- $errors[] = "Cannot open " . $langid['file'] . "for reading $php_errormsg";
- } else {
- $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) {
- if (!$_REQUEST['dryrun']) {
- $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";
- $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]));
- $db->query("SELECT * FROM template_words WHERE
- wordref = '$matches[1]' AND
- language = '$langid'
- ");
- if ($db->num_rows()) {
- if (!$matches[2]) {
- if (!$_REQUEST['dryrun']) {
- $db->query("DELETE FROM template_words
- WHERE wordref = '$matches[1]' AND
- language = '$landid'
- ");
- }
- $imported[$matches[1]][$langid] = -4;
- }
- if (!$_REQUEST['dryrun'] AND !$_REQUEST['replace_all']) {
- $db->query("
- UPDATE template_words SET
- category = '$catid',
- text = '$matches[2]'
- WHERE
- wordref = '$matches[1]'
- AND language = '$langid'
- ");
- if ($db->affected_rows()) {
- $imported[$matches[1]][$langid] = -1;
- } else {
- $imported[$matches[1]][$langid] = -2;
- }
- } else {
- if ($imported[$matches[1]][$langid] == 0) {
- $imported[$matches[1]][$langid] = -1;
- }
- }
- } else {
- if (!$_REQUEST['dryrun']) {
- $db->query("INSERT INTO template_words (wordref, category, text, language)
- VALUES ('$matches[1]', '$catid', '$matches[2]', '$langid')
- ");
- }
- $imported[$matches[1]][$langid] = -3;
- }
- } else {
- $errors[] = "Error in file " . $languages[$langid]['file'] . " on line $count.";
- }
- }
- }
- }
- }
-
- $cols = array('Template Name');
- foreach ($languages as $lang) {
- $cols[] = $lang['name'];
- }
- foreach ($imported AS $template => $val) {
- $row = array($template);
- foreach ($languages AS $lang_id => $lang) {
- if ($val[$lang_id] == -1) {
- $result = iff($_REQUEST['dryrun'], '<FONT COLOR="green">Would Import</FONT>', '<FONT COLOR="green">Imported</FONT>');
- } elseif ($val[$lang_id] == -2) {
- $result = '<FONT COLOR="green">No Change</FONT>';
- } elseif ($val[$lang_id] == -3) {
- $result = iff($_REQUEST['dryrun'], '<FONT COLOR="green">Would Add</FONT>', '<FONT COLOR="green">Added</FONT>');
- } elseif ($val[$lang_id] == -4) {
- $result = iff($_REQUEST['dryrun'], '<FONT COLOR="red">Would Delete</FONT>', '<FONT COLOR="red">Deleted</FONT>');
- } elseif ($val[$lang_id] === FALSE) {
- $result = '<FONT COLOR="red">Error</FONT>';
- } else {
- $result = 'Not defined';
- }
- $row[] = "<CENTER>$result</CENTER>";
- if (is_string($val[$lang_id])) {
- $errors[] = $val[$lang_id];
- }
- }
- $rows[] = $row;
- }
- }
- table_header('Import Results');
- table_content($cols, $rows);
- table_footer();
- if (count($errors)) {
- table_header('Import Errors');
- table_content(NULL, $errors);
- table_footer();
- }
- return;
- }
- /*****************************************************
- function dev_install_words_default()
- -----DESCRIPTION: -----------------------------------
- Imports all language translations.
- -----ARGUMENTS: -------------------------------------
- location Directory to import from
- -----RETURNS: ---------------------------------------
- Nothing.
- *****************************************************/
- function dev_install_words_default() {
- global $db;
- $db->query("SELECT * FROM template_words WHERE language = '1'");
- while ($result = $db->row_array()) {
- $data[] = array(
- $result['wordref'],
- 0,
- $result['text'],
- $result['category'],
- 0
- );
- }
- $db->query("
- INSERT INTO template_words
- (wordref, language, text, category, cust)
- VALUES " . multi_array2sql($data) . "
- ");
- }
- /*****************************************************
- 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_errormsgn";
- }
- if (!@is_dir($location)) {
- $errors[] .= "[ERROR] $location isn't a directory or can't be stat()'d; cannot import e-mail templates: $php_errormsgn";
- }
- $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_errormsgn";
- 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_errormsgn";
- continue;
- }
- if (!$dirhandle = opendir($languages[$lang_id]['dir'])) {
- $errors .= "[ERROR] " . $languages[$lang_id]['dir'] . " exists but can't be opened for reading: $php_errormsgn";
- 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_errormsgn";
- }
- }
- }
- 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_errormsgn";
- }
- if (!@is_dir($location)) {
- $errors .= "[ERROR] $location isn't a directory or can't be stat()'d; cannot import templates: $php_errormsgn";
- }
- $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_errormsgn";
- 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 #$catidn";
- $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 - $wordrefn";
- }
- }
- $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("nn", $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;
- }