Ejemplo n.º 1
0
function importFiles($parent, $filedir, $files, $mode)
{
    global $modx;
    global $_lang, $allowedfiles;
    global $search_default, $cache_default, $publish_default;
    $createdon = time();
    $createdby = $modx->getLoginUserID();
    if (!is_array($files)) {
        return;
    }
    if ($_POST['object'] == 'all') {
        $modx->config['default_template'] = '0';
        $richtext = '0';
    } else {
        $richtext = '1';
    }
    foreach ($files as $id => $value) {
        if (is_array($value)) {
            // create folder
            $alias = $id;
            printf('<span>' . $_lang['import_site_importing_document'] . '</span>', $alias);
            foreach (array('index.html', 'index.htm') as $filename) {
                $filepath = $filedir . $alias . '/' . $filename;
                if (file_exists($filepath)) {
                    $file = getFileContent($filepath);
                    list($pagetitle, $content, $description) = treatContent($file, $filename, $alias);
                    $date = filemtime($filepath);
                    $field = array();
                    $field['type'] = 'document';
                    $field['contentType'] = 'text/html';
                    $field['pagetitle'] = $pagetitle;
                    $field['longtitle'] = $pagetitle;
                    $field['description'] = $description;
                    $field['alias'] = $modx->stripAlias($alias);
                    $field['published'] = $publish_default;
                    $field['parent'] = $parent;
                    $field['content'] = $modx->db->escape($content);
                    $field['richtext'] = $richtext;
                    $field['template'] = $modx->config['default_template'];
                    $field['searchable'] = $search_default;
                    $field['cacheable'] = $cache_default;
                    $field['createdby'] = $createdby;
                    $field['createdon'] = $date;
                    $field['editedon'] = $date;
                    $field['isfolder'] = 1;
                    $field['menuindex'] = 1;
                    $newid = $modx->db->insert($field, '[+prefix+]site_content');
                    if ($newid) {
                        echo ' - <span class="success">' . $_lang['import_site_success'] . '</span><br />' . "\n";
                        importFiles($newid, $filedir . $alias . '/', $value, 'sub');
                    } else {
                        echo '<span class="fail">' . $_lang["import_site_failed"] . "</span> " . $_lang["import_site_failed_db_error"] . $modx->db->getLastError();
                        exit;
                    }
                    break;
                }
            }
        } else {
            // create document
            if ($mode == 'sub' && $value == 'index.html') {
                continue;
            }
            $filename = $value;
            $fparts = explode('.', $value);
            $alias = $fparts[0];
            $ext = count($fparts) > 1 ? $fparts[count($fparts) - 1] : "";
            printf("<span>" . $_lang['import_site_importing_document'] . "</span>", $filename);
            if (!in_array($ext, $allowedfiles)) {
                echo ' - <span class="fail">' . $_lang["import_site_skip"] . '</span><br />' . "\n";
            } else {
                $filepath = $filedir . $filename;
                $file = getFileContent($filepath);
                list($pagetitle, $content, $description) = treatContent($file, $filename, $alias);
                $date = filemtime($filepath);
                $field = array();
                $field['type'] = 'document';
                $field['contentType'] = 'text/html';
                $field['pagetitle'] = $pagetitle;
                $field['longtitle'] = $pagetitle;
                $field['description'] = $description;
                $field['alias'] = $modx->stripAlias($alias);
                $field['published'] = $publish_default;
                $field['parent'] = $parent;
                $field['content'] = $modx->db->escape($content);
                $field['richtext'] = $richtext;
                $field['template'] = $modx->config['default_template'];
                $field['searchable'] = $search_default;
                $field['cacheable'] = $cache_default;
                $field['createdby'] = $createdby;
                $field['createdon'] = $date;
                $field['editedon'] = $date;
                $field['isfolder'] = 0;
                $field['menuindex'] = $alias == 'index' ? 0 : 2;
                $newid = $modx->db->insert($field, '[+prefix+]site_content');
                if ($newid) {
                    echo ' - <span class="success">' . $_lang['import_site_success'] . '</span><br />' . "\n";
                } else {
                    echo '<span class="fail">' . $_lang["import_site_failed"] . "</span> " . $_lang["import_site_failed_db_error"] . $modx->db->getLastError();
                    exit;
                }
                $is_site_start = false;
                if ($filename == 'index.html') {
                    $is_site_start = true;
                }
                if ($is_site_start == true && $_POST['reset'] == 'on') {
                    $modx->db->update("setting_value={$newid}", '[+prefix+]system_settings', "setting_name='site_start'");
                    $modx->db->update('menuindex=0', '[+prefix+]site_content', "id='{$newid}'");
                }
            }
        }
    }
}
Ejemplo n.º 2
0
function importFiles($parent, $filepath, $files)
{
    global $modx;
    global $_lang, $allowedfiles;
    global $dbase, $table_prefix;
    global $default_template, $search_default, $cache_default, $publish_default;
    $createdon = time();
    $createdby = $modx->getLoginUserID();
    if (!is_array($files)) {
        return;
    }
    foreach ($files as $id => $value) {
        if (is_array($value)) {
            // create folder
            $alias = !isset($modx->documentListing[$id]) ? $id : $id . '-' . substr(uniqid(''), -3);
            $modx->documentListing[$alias] = true;
            printf("<p>" . $_lang['import_site_importing_document'] . "</p>", $id);
            $sql = "INSERT INTO {$dbase}.`" . $table_prefix . "site_content`\n                   (type, contentType, pagetitle, alias, published, parent, isfolder, content, template, menuindex, searchable, cacheable, createdby, createdon) VALUES\n                   ('document', 'text/html', '" . $modx->db->escape($id) . "', '" . $modx->stripAlias($alias) . "', " . $publish_default . ", '{$parent}', 1, '', '" . $default_template . "', 0, " . $search_default . ", " . $cache_default . ", {$createdby}, {$createdon});";
            $rs = mysql_query($sql);
            if ($rs) {
                $new_parent = mysql_insert_id();
            } else {
                echo "<p>" . $_lang["import_site_failed_db_error"] . mysql_error() . "</p>";
                exit;
            }
            echo "<p class=\"success\">" . $_lang["import_site_success"] . "</p>";
            importFiles($new_parent, $filepath . "/{$id}/", $value);
        } else {
            // create dcoument
            $filename = $value;
            $fparts = explode(".", $value);
            $value = $fparts[0];
            $ext = count($fparts) > 1 ? $fparts[count($fparts) - 1] : "";
            printf("<p>" . $_lang['import_site_importing_document'] . "</p>", $filename);
            $alias = !isset($modx->documentListing[$value]) ? $value : $value . '-' . substr(uniqid(''), -3);
            $modx->documentListing[$alias] = true;
            if (!in_array($ext, $allowedfiles)) {
                echo "<p class=\"success\">" . $_lang["import_site_skip"] . "</p>";
            } else {
                $file = getFileContent("{$filepath}/{$filename}");
                if (preg_match("/<title>(.*)<\\/title>/i", $file, $matches)) {
                    $pagetitle = $matches[1];
                } else {
                    $pagetitle = $value;
                }
                if (!$pagetitle) {
                    $pagetitle = $value;
                }
                if (preg_match("/<body[^>]*>(.*)[^<]+<\\/body>/is", $file, $matches)) {
                    $content = $matches[1];
                } else {
                    $content = $file;
                }
                $sql = "INSERT INTO {$dbase}.`" . $table_prefix . "site_content`\n                       (type, contentType, pagetitle, alias, published, parent, isfolder, content, template, menuindex, searchable, cacheable, createdby, createdon) VALUES\n                       ('document', 'text/html', '" . $modx->db->escape($pagetitle) . "', '" . $modx->stripAlias($alias) . "', " . $publish_default . ", '{$parent}', 0, '" . $modx->db->escape($content) . "', '" . $default_template . "', 0, " . $search_default . ", " . $cache_default . ", {$createdby}, {$createdon});";
                $rs = mysql_query($sql);
                if (!$rs) {
                    echo "<p><span class=\"fail\">" . $_lang["import_site_failed"] . "</span> " . $_lang["import_site_failed_db_error"] . mysql_error() . "</p>";
                    exit;
                }
                echo "<p class=\"success\">" . $_lang["import_site_success"] . "</p>";
            }
        }
    }
}
Ejemplo n.º 3
0
 function importFiles(&$modx, &$results, $allowedfiles, $parent, $filepath, $files, $context = 'web')
 {
     if (!is_array($files)) {
         return;
     }
     if ($parent > 0) {
         if ($parentResource = $modx->getObject('modResource', $parent)) {
             $context = $parentResource->get('context_key');
             $parentResource->set('isfolder', true);
             $parentResource->save();
         } else {
             $results .= "Could not get parent ({$parent}) resource to set isfolder attribute after import.";
             return;
         }
     }
     foreach ($files as $id => $value) {
         if (is_array($value)) {
             /* create folder */
             $resource = $modx->newObject('modDocument');
             $resource->set('context_key', $context);
             $resource->set('content_type', 1);
             $resource->set('pagetitle', $id);
             $resource->set('parent', $parent);
             $resource->set('isfolder', true);
             $alias = getResourceAlias($modx, $resource, $results, $id, $parent, $context);
             $resource->set('alias', $alias);
             $resource->set('published', false);
             $resource->set('template', $modx->getOption('default_template'));
             $resource->set('menuindex', $modx->getCount('modResource', array('parent' => $parent)));
             $resource->set('searchable', $modx->getOption('search_default'));
             $resource->set('cacheable', $modx->getOption('cache_default'));
             $results .= sprintf($modx->lexicon('import_site_importing_document'), $alias);
             if (!$resource->save()) {
                 $results .= "Could not import resource from {$filepath}/{$id}: <br />" . nl2br(print_r($modx->errorInfo(), true));
             } else {
                 $results .= $modx->lexicon('import_site_success') . "<br />";
                 importFiles($modx, $results, $allowedfiles, $resource->get('id'), $filepath . "/{$id}/", $value, $context);
             }
         } else {
             /* create resource */
             $filename = $value;
             $fparts = explode(".", $value);
             $value = $fparts[0];
             $ext = count($fparts) > 1 ? $fparts[count($fparts) - 1] : "";
             $results .= sprintf($modx->lexicon('import_site_importing_document'), $filename);
             if (!in_array($ext, $allowedfiles)) {
                 $results .= $modx->lexicon('import_site_skip') . "<br />";
             } else {
                 $file = getFileContent($modx, $results, "{$filepath}/{$filename}");
                 if (preg_match("/<title>(.*)<\\/title>/i", $file, $matches)) {
                     $pagetitle = $matches[1];
                 } else {
                     $pagetitle = $value;
                 }
                 if (!$pagetitle) {
                     $pagetitle = $value;
                 }
                 if (preg_match("/<body[^>]*>(.*)[^<]+<\\/body>/is", $file, $matches)) {
                     $content = $matches[1];
                 } else {
                     $content = $file;
                 }
                 $resource = $modx->newObject('modDocument');
                 $resource->set('context_key', $context);
                 $resource->set('content_type', 1);
                 $resource->set('pagetitle', $pagetitle);
                 $resource->set('parent', $parent);
                 $resource->set('isfolder', false);
                 $alias = getResourceAlias($modx, $resource, $results, $value, $parent, $context);
                 $resource->set('alias', $alias);
                 $resource->set('published', false);
                 $resource->set('template', $modx->getOption('default_template'));
                 $resource->set('menuindex', $modx->getCount('modResource', array('parent' => $parent)));
                 $resource->set('searchable', $modx->getOption('search_default'));
                 $resource->set('cacheable', $modx->getOption('cache_default'));
                 $resource->set('content', $content);
                 if (!$resource->save()) {
                     $results .= $modx->lexicon('import_site_failed') . "Could not import resource from {$filepath}/{$filename}: <br />" . nl2br(print_r($modx->errorInfo(), true));
                 } else {
                     $results .= $modx->lexicon('import_site_success') . "<br />";
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
// 1) Only "standard" individual events are imported. Events without
//    age groups such as push hands and group events will be skipped
//    and must be added manually.
// 2) Weight for push hands competitors will need to be entered manually
//    by looking at which push hands event is listed under "Events".
//
// When running the script, it is best to save the output to a file
// to maintain a record of additional manual work that needs to be done.
require_once '../util/Db.php';
require_once '../util/TextUtils.php';
$fileNames = getFileNames();
if (count($fileNames) == 0) {
    print "No input file(s).\n";
    exit(1);
} else {
    importFiles($fileNames);
    exit(0);
}
/**
 * File names passed in through command line parameters.
 * @return array
 */
function getFileNames()
{
    global $argv;
    if (count($argv) > 1) {
        return array_slice($argv, 1);
    } else {
        return array();
    }
}