Ejemplo n.º 1
0
function publications_admin_updateconfig()
{
    // Confirm authorisation code
    if (!xarSecConfirmAuthKey()) {
        return;
    }
    // Get parameters
    //A lot of these probably are bools, still might there be a need to change the template to return
    //'true' and 'false' to use those...
    if (!xarVarFetch('settings', 'array', $settings, array(), XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('usetitleforurl', 'int', $usetitleforurl, xarModVars::get('publications', 'usetitleforurl'), XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('defaultstate', 'isset', $defaultstate, 0, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('defaultsort', 'isset', $defaultsort, 'date', XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('usealias', 'int', $usealias, 0, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('ptid', 'isset', $ptid, xarModVars::get('publications', 'defaultpubtype'), XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('multilanguage', 'int', $multilanguage, 0, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('tab', 'str:1:10', $data['tab'], 'global', XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarSecurityCheck('AdminPublications', 1, 'Publication', "{$ptid}:All:All:All")) {
        return;
    }
    if ($data['tab'] == 'global') {
        if (!xarVarFetch('defaultpubtype', 'isset', $defaultpubtype, 1, XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!xarVarFetch('sortpubtypes', 'isset', $sortpubtypes, 'id', XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!xarVarFetch('defaultlanguage', 'str:1:100', $defaultlanguage, xarModVars::get('publications', 'defaultlanguage'), XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!xarVarFetch('debugmode', 'checkbox', $debugmode, xarModVars::get('publications', 'debugmode'), XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!xarVarFetch('defaultfrontpage', 'str', $defaultfrontpage, xarModVars::get('publications', 'defaultfrontpage'), XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!xarVarFetch('defaultbackpage', 'str', $defaultbackpage, xarModVars::get('publications', 'defaultbackpage'), XARVAR_NOT_REQUIRED)) {
            return;
        }
        xarModVars::set('publications', 'defaultpubtype', $defaultpubtype);
        xarModVars::set('publications', 'sortpubtypes', $sortpubtypes);
        xarModVars::set('publications', 'defaultlanguage', $defaultlanguage);
        xarModVars::set('publications', 'debugmode', $debugmode);
        xarModVars::set('publications', 'usealias', $usealias);
        xarModVars::set('publications', 'usetitleforurl', $usetitleforurl);
        xarModVars::set('publications', 'defaultfrontpage', $defaultfrontpage);
        xarModVars::set('publications', 'defaultbackpage', $defaultbackpage);
        // Allow multilanguage only if the languages property is present
        sys::import('modules.dynamicdata.class.properties.registration');
        $types = PropertyRegistration::Retrieve();
        if (isset($types[30039])) {
            xarModVars::set('publications', 'multilanguage', $multilanguage);
        } else {
            xarModVars::set('publications', 'multilanguage', 0);
        }
        // Get the special pages.
        foreach (array('defaultpage', 'errorpage', 'notfoundpage', 'noprivspage') as $special_name) {
            unset($special_id);
            if (!xarVarFetch($special_name, 'id', $special_id, 0, XARVAR_NOT_REQUIRED)) {
                return;
            }
            xarModVars::set('publications', $special_name, $special_id);
        }
        if (xarDB::getType() == 'mysql') {
            if (!xarVarFetch('fulltext', 'isset', $fulltext, '', XARVAR_NOT_REQUIRED)) {
                return;
            }
            $oldval = xarModVars::get('publications', 'fulltextsearch');
            $index = 'i_' . xarDB::getPrefix() . '_publications_fulltext';
            if (empty($fulltext) && !empty($oldval)) {
                // Get database setup
                $dbconn = xarDB::getConn();
                $xartable = xarDB::getTables();
                $publicationstable = $xartable['publications'];
                // Drop fulltext index on publications table
                $query = "ALTER TABLE {$publicationstable} DROP INDEX {$index}";
                $result =& $dbconn->Execute($query);
                if (!$result) {
                    return;
                }
                xarModVars::set('publications', 'fulltextsearch', '');
            } elseif (!empty($fulltext) && empty($oldval)) {
                $searchfields = array('title', 'description', 'summary', 'body1', 'notes');
                //                $searchfields = explode(',',$fulltext);
                // Get database setup
                $dbconn = xarDB::getConn();
                $xartable = xarDB::getTables();
                $publicationstable = $xartable['publications'];
                // Add fulltext index on publications table
                $query = "ALTER TABLE {$publicationstable} ADD FULLTEXT {$index} (" . join(', ', $searchfields) . ")";
                $result =& $dbconn->Execute($query);
                if (!$result) {
                    return;
                }
                xarModVars::set('publications', 'fulltextsearch', join(',', $searchfields));
            }
        }
        // Module settings
        $data['module_settings'] = xarMod::apiFunc('base', 'admin', 'getmodulesettings', array('module' => 'publications'));
        $data['module_settings']->setFieldList('items_per_page, use_module_alias, module_alias_name, enable_short_urls, user_menu_link', 'use_module_icons');
        $isvalid = $data['module_settings']->checkInput();
        if (!$isvalid) {
            return xarTplModule('base', 'admin', 'modifyconfig', $data);
        } else {
            $itemid = $data['module_settings']->updateItem();
        }
        // Pull the base category ids from the template and save them
        $picker = DataPropertyMaster::getProperty(array('name' => 'categorypicker'));
        $picker->checkInput('basecid');
    } elseif ($data['tab'] == 'pubtypes') {
        // Get the publication type for this display and save the settings to it
        $pubtypeobject = DataObjectMaster::getObject(array('name' => 'publications_types'));
        $pubtypeobject->getItem(array('itemid' => $ptid));
        $configsettings = $pubtypeobject->properties['configuration']->getValue();
        $checkbox = DataPropertyMaster::getProperty(array('name' => 'checkbox'));
        $boxes = array('show_hitount', 'show_ratings', 'show_keywords', 'show_comments', 'show_prevnext', 'show_archives', 'show_publinks', 'show_pubcount', 'show_map', 'prevnextart', 'dot_transform', 'title_transform', 'show_categories', 'show_catcount', 'show_prevnext', 'allow_translations');
        foreach ($boxes as $box) {
            $isvalid = $checkbox->checkInput($box);
            if ($isvalid) {
                $settings[$box] = $checkbox->value;
            }
        }
        //        foreach ($configsettings as $key => $value)
        //            if (!isset($settings[$key])) $settings[$key] = 0;
        $isvalid = true;
        // Get the default access rules
        $access = DataPropertyMaster::getProperty(array('name' => 'access'));
        $validprop = $access->checkInput("access_add");
        $addaccess = $access->value;
        $isvalid = $isvalid && $validprop;
        $validprop = $access->checkInput("access_display");
        $displayaccess = $access->value;
        $isvalid = $isvalid && $validprop;
        $validprop = $access->checkInput("access_modify");
        $modifyaccess = $access->value;
        $isvalid = $isvalid && $validprop;
        $validprop = $access->checkInput("access_delete");
        $deleteaccess = $access->value;
        $isvalid = $isvalid && $validprop;
        $allaccess = array('add' => $addaccess, 'display' => $displayaccess, 'modify' => $modifyaccess, 'delete' => $deleteaccess);
        $pubtypeobject->properties['access']->setValue(serialize($allaccess));
        $pubtypeobject->properties['configuration']->setValue(serialize($settings));
        $pubtypeobject->updateItem(array('itemid' => $ptid));
        $pubtypes = xarModAPIFunc('publications', 'user', 'get_pubtypes');
        if ($usealias) {
            xarModSetAlias($pubtypes[$ptid]['name'], 'publications');
        } else {
            xarModDelAlias($pubtypes[$ptid]['name'], 'publications');
        }
    } elseif ($data['tab'] == 'redirects') {
        $redirects = DataPropertyMaster::getProperty(array('name' => 'array'));
        $redirects->display_column_definition['value'] = array(array("From", "To"), array(2, 2), array("", ""), array("", ""));
        $isvalid = $redirects->checkInput("redirects");
        xarModVars::set('publications', 'redirects', $redirects->value);
    }
    xarController::redirect(xarModURL('publications', 'admin', 'modifyconfig', array('ptid' => $ptid, 'tab' => $data['tab'])));
    return true;
}
Ejemplo n.º 2
0
/**
 * Import an object definition or an object item from XML
 */
function publications_adminapi_importpubtype($args)
{
    // Security check - we require ADMIN rights here
    if (!xarSecurityCheck('AdminPublications')) {
        return;
    }
    extract($args);
    if (empty($xml) && empty($file)) {
        $msg = xarML('Missing import file or XML content');
        throw new BadParameterException(null, $msg);
    } elseif (!empty($file) && (!file_exists($file) || !preg_match('/\\.xml$/', $file))) {
        $msg = xarML('Invalid import file');
        throw new BadParameterException(null, $msg);
    }
    $pubtypes = xarModAPIFunc('publications', 'user', 'get_pubtypes');
    $proptypes = DataPropertyMaster::getPropertyTypes();
    $name2id = array();
    foreach ($proptypes as $propid => $proptype) {
        $name2id[$proptype['name']] = $propid;
    }
    $prefix = xarDB::getPrefix();
    $prefix .= '_';
    if (!empty($file)) {
        $fp = @fopen($file, 'r');
        if (!$fp) {
            $msg = xarML('Unable to open import file');
            throw new BadParameterException(null, $msg);
        }
    } else {
        $lines = preg_split("/\r?\n/", $xml);
        $maxcount = count($lines);
    }
    $what = '';
    $count = 0;
    $ptid = 0;
    $objectname2objectid = array();
    $objectcache = array();
    $objectmaxid = array();
    while (!empty($file) && !feof($fp) || !empty($xml) && $count < $maxcount) {
        if (!empty($file)) {
            $line = fgets($fp, 4096);
        } else {
            $line = $lines[$count];
        }
        $count++;
        if (empty($what)) {
            if (preg_match('#<object name="(\\w+)">#', $line, $matches)) {
                // in case we import the object definition
                $object = array();
                $object['name'] = $matches[1];
                $what = 'object';
            } elseif (preg_match('#<items>#', $line)) {
                // in case we only import data
                $what = 'item';
            }
        } elseif ($what == 'object') {
            if (preg_match('#<([^>]+)>(.*)</\\1>#', $line, $matches)) {
                $key = $matches[1];
                $value = $matches[2];
                if (isset($object[$key])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Duplicate definition for #(1) key #(2) on line #(3)', 'object', xarVarPrepForDisplay($key), $count);
                    throw new DuplicateException(null, $msg);
                }
                $object[$key] = $value;
            } elseif (preg_match('#<config>#', $line)) {
                if (isset($object['config'])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Duplicate definition for #(1) key #(2) on line #(3)', 'object', 'config', $count);
                    throw new DuplicateException(null, $msg);
                }
                $config = array();
                $what = 'config';
            } elseif (preg_match('#<properties>#', $line)) {
                if (empty($object['name']) || empty($object['moduleid'])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Missing keys in object definition');
                    throw new BadParameterException(null, $msg);
                }
                // make sure we drop the object id, because it might already exist here
                unset($object['objectid']);
                $properties = array();
                $what = 'property';
            } elseif (preg_match('#<items>#', $line)) {
                $what = 'item';
            } elseif (preg_match('#</object>#', $line)) {
                $what = '';
            } else {
                // multi-line entries not relevant here
            }
        } elseif ($what == 'config') {
            if (preg_match('#<([^>]+)>(.*)</\\1>#', $line, $matches)) {
                $key = $matches[1];
                $value = $matches[2];
                if (isset($config[$key])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Duplicate definition for #(1) key #(2) on line #(3)', 'config', xarVarPrepForDisplay($key), $count);
                    throw new DuplicateException(null, $msg);
                }
                $config[$key] = $value;
            } elseif (preg_match('#</config>#', $line)) {
                // override default view if necessary
                $config['defaultview'] = 1;
                $object['config'] = serialize($config);
                $config = array();
                $what = 'object';
            } else {
                // multi-line entries not relevant here
            }
        } elseif ($what == 'property') {
            if (preg_match('#<property name="(\\w+)">#', $line, $matches)) {
                $property = array();
                $property['name'] = $matches[1];
            } elseif (preg_match('#</property>#', $line)) {
                if (empty($property['name']) || empty($property['type'])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Missing keys in property definition');
                    throw new BadParameterException(null, $msg);
                }
                // make sure we drop the property id, because it might already exist here
                unset($property['id']);
                // TODO: watch out for multi-sites
                // replace default xar_* table prefix with local one
                $property['source'] = preg_replace("/^xar_/", $prefix, $property['source']);
                // add this property to the list
                $properties[] = $property;
            } elseif (preg_match('#<([^>]+)>(.*)</\\1>#', $line, $matches)) {
                $key = $matches[1];
                $value = $matches[2];
                if (isset($property[$key])) {
                    if (!empty($file)) {
                        fclose($fp);
                    }
                    $msg = xarML('Duplicate definition for #(1) key #(2) on line #(3)', 'property', xarVarPrepForDisplay($key), $count);
                    throw new DuplicateException(null, $msg);
                }
                $property[$key] = $value;
            } elseif (preg_match('#</properties>#', $line)) {
                // 1. make sure we have a unique pubtype name
                foreach ($pubtypes as $pubid => $pubtype) {
                    if ($object['name'] == $pubtype['name']) {
                        $object['name'] .= '_' . time();
                        break;
                    }
                }
                // 2. fill in the pubtype field config
                $fields = array();
                $extra = array();
                foreach ($properties as $property) {
                    $field = $property['name'];
                    switch ($field) {
                        case 'id':
                        case 'pubtype_id':
                            // skip these
                            break;
                        case 'title':
                        case 'summary':
                        case 'body':
                        case 'notes':
                        case 'owner':
                        case 'pubdate':
                        case 'state':
                            // convert property type to string if necessary
                            if (is_numeric($property['type'])) {
                                if (isset($proptypes[$property['type']])) {
                                    $property['type'] = $proptypes[$property['type']]['name'];
                                } else {
                                    $property['type'] = 'static';
                                }
                            }
                            // reset disabled field labels to empty
                            if (empty($property['state'])) {
                                $property['label'] = '';
                            }
                            if (!isset($property['validation'])) {
                                $property['validation'] = '';
                            }
                            $fields[$field] = array('label' => $property['label'], 'format' => $property['type'], 'input' => $property['input'], 'validation' => $property['validation']);
                            break;
                        default:
                            // convert property type to numeric if necessary
                            if (!is_numeric($property['type'])) {
                                if (isset($name2id[$property['type']])) {
                                    $property['type'] = $name2id[$property['type']];
                                } else {
                                    $property['type'] = 1;
                                }
                            }
                            $extra[] = $property;
                            break;
                    }
                }
                // 3. create the pubtype
                $ptid = xarModAPIFunc('publications', 'admin', 'createpubtype', array('name' => $object['name'], 'descr' => $object['label'], 'config' => $fields));
                if (empty($ptid)) {
                    return;
                }
                // 4. set the module variables
                xarModVars::set('publications', 'settings.' . $ptid, $object['config']);
                xarModVars::set('publications', 'number_of_categories.' . $ptid, 0);
                xarModVars::set('publications', 'mastercids.' . $ptid, '');
                // 5. create a dynamic object if necessary
                if (count($extra) > 0) {
                    $object['itemtype'] = $ptid;
                    $object['config'] = '';
                    $object['isalias'] = 0;
                    $objectid = xarModAPIFunc('dynamicdata', 'admin', 'createobject', $object);
                    if (!isset($objectid)) {
                        if (!empty($file)) {
                            fclose($fp);
                        }
                        return;
                    }
                    // 6. create the dynamic properties
                    foreach ($extra as $property) {
                        $property['objectid'] = $objectid;
                        $property['moduleid'] = $object['moduleid'];
                        $property['itemtype'] = $object['itemtype'];
                        $prop_id = xarModAPIFunc('dynamicdata', 'admin', 'createproperty', $property);
                        if (!isset($prop_id)) {
                            if (!empty($file)) {
                                fclose($fp);
                            }
                            return;
                        }
                    }
                    // 7. check if we need to enable DD hooks for this pubtype
                    if (!xarModIsHooked('dynamicdata', 'publications')) {
                        xarModAPIFunc('modules', 'admin', 'enablehooks', array('callerModName' => 'publications', 'callerItemType' => $ptid, 'hookModName' => 'dynamicdata'));
                    }
                }
                $properties = array();
                $what = 'object';
            } elseif (preg_match('#<items>#', $line)) {
                $what = 'item';
            } elseif (preg_match('#</object>#', $line)) {
                $what = '';
            } else {
                // multi-line entries not relevant here
            }
        } elseif ($what == 'item') {
            /* skip this for publications
                        if (preg_match('#<([^> ]+) itemid="(\d+)">#',$line,$matches)) {
                            // find out what kind of item we're dealing with
                            $objectname = $matches[1];
                            $itemid = $matches[2];
                            if (empty($objectname2objectid[$objectname])) {
                                $objectinfo = DataObjectMaster::getObjectInfo(array('name' => $objectname));
                                if (isset($objectinfo) && !empty($objectinfo['objectid'])) {
                                    $objectname2objectid[$objectname] = $objectinfo['objectid'];
                                } else {
                                    if (!empty($file)) fclose($fp);
                                    $msg = xarML('Unknown #(1) "#(2)" on line #(3)','object',xarVarPrepForDisplay($objectname),$count);
                                    throw new BadParameterException(null, $msg);
                                }
                            }
                            $objectid = $objectname2objectid[$objectname];
                            $item = array();
                            // don't save the item id for now...
                        // TODO: keep the item id if we set some flag
                            //$item['itemid'] = $itemid;
                            $closeitem = $objectname;
                            $closetag = 'N/A';
                        } elseif (preg_match("#</$closeitem>#",$line)) {
                            // let's create the item now...
                            if (!isset($objectcache[$objectid])) {
                                $objectcache[$objectid] = new DataObject(array('objectid' => $objectid));
                            }
                            // set the item id to 0
                        // TODO: keep the item id if we set some flag
                            $item['itemid'] = 0;
                            // create the item
                            $itemid = $objectcache[$objectid]->createItem($item);
                            if (empty($itemid)) {
                                if (!empty($file)) fclose($fp);
                                return;
                            }
                            // keep track of the highest item id
                            if (empty($objectmaxid[$objectid]) || $objectmaxid[$objectid] < $itemid) {
                                $objectmaxid[$objectid] = $itemid;
                            }
                            $closeitem = 'N/A';
                            $closetag = 'N/A';
                        } elseif (preg_match('#<([^>]+)>(.*)</\1>#',$line,$matches)) {
                            $key = $matches[1];
                            $value = $matches[2];
                            if (isset($item[$key])) {
                                if (!empty($file)) fclose($fp);
                                $msg = xarML('Duplicate definition for #(1) key #(2) on line #(3)','item',xarVarPrepForDisplay($key),$count);
                                throw new DuplicateException(null, $msg);
                            }
                            $item[$key] = $value;
                            $closetag = 'N/A';
                        } elseif (preg_match('#<([^/>]+)>(.*)#',$line,$matches)) {
                            // multi-line entries *are* relevant here
                            $key = $matches[1];
                            $value = $matches[2];
                            if (isset($item[$key])) {
                                if (!empty($file)) fclose($fp);
                                $msg = xarML('Duplicate definition for #(1) key #(2)','item',xarVarPrepForDisplay($key));
                                throw new DuplicateException(null, $msg);
                            }
                            $item[$key] = $value;
                            $closetag = $key;
                        } elseif (preg_match("#(.*)</$closetag>#",$line,$matches)) {
                            // multi-line entries *are* relevant here
                            $value = $matches[1];
                            if (!isset($item[$closetag])) {
                                if (!empty($file)) fclose($fp);
                                $msg = xarML('Undefined #(1) key #(2)','item',xarVarPrepForDisplay($closetag));
                                throw new BadParameterException(null, $msg);
                            }
                            $item[$closetag] .= $value;
                            $closetag = 'N/A';
                        } elseif ($closetag != 'N/A') {
                            // multi-line entries *are* relevant here
                            if (!isset($item[$closetag])) {
                                if (!empty($file)) fclose($fp);
                                $msg = xarML('Undefined #(1) key #(2)','item',xarVarPrepForDisplay($closetag));
                                throw new BadParameterException(null, $msg);
                            }
                            $item[$closetag] .= $line;
                        } elseif (preg_match('#</items>#',$line)) {
            skip this for publications */
            if (preg_match('#</items>#', $line)) {
                $what = 'object';
            } elseif (preg_match('#</object>#', $line)) {
                $what = '';
            } else {
            }
        } else {
        }
    }
    if (!empty($file)) {
        fclose($fp);
    }
    return $ptid;
}