/**
 * Cleans a link to a section of a page, which
 * means that only the part of the page name is
 * cleaned.
 *
 * @param  String $name Link to section of a page
 * @return String       Clean name.
 */
function wiki_clean_section_link($name)
{
    if (ereg('(.*)+(##)(.*)+', $name)) {
        $at = '##';
    } else {
        $at = '#';
    }
    $parts = explode($at, $name, 2);
    $pagename = wiki_clean_name($parts[0]);
    $sectionname = $parts[1];
    return $pagename . $at . $sectionname;
}
function wiki_update_instance($dfwiki)
{
    global $CFG;
    /// Given an object containing all the necessary data,
    /// (defined by the form in mod.html) this function
    /// will update an existing instance with new data.
    $dfwiki->timemodified = time();
    $dfwiki->id = $dfwiki->instance;
    # May have to add extra stuff in here #
    $dfwiki->pagename = trim($dfwiki->pagename);
    $dfwiki->pagename = stripslashes($dfwiki->pagename);
    $dfwiki->pagename = wiki_clean_name($dfwiki->pagename);
    $dfwiki->pagename = addslashes($dfwiki->pagename);
    if (!$dfwiki->pagename) {
        $dfwiki->pagename = get_string('firstpage', 'wiki');
    }
    //privileges
    $dfwiki->editable = isset($dfwiki->editable) ? 1 : 0;
    $dfwiki->attach = isset($dfwiki->attach) ? 1 : 0;
    $dfwiki->restore = isset($dfwiki->restore) ? 1 : 0;
    $dfwiki->teacherdiscussion = isset($dfwiki->teacherdiscussion) ? 1 : 0;
    $dfwiki->studentdiscussion = isset($dfwiki->studentdiscussion) ? 1 : 0;
    $dfwiki->editanothergroup = isset($dfwiki->editanothergroup) ? 1 : 0;
    $dfwiki->editanotherstudent = isset($dfwiki->editanotherstudent) ? 1 : 0;
    $dfwiki->votemode = isset($dfwiki->votemode) ? 1 : 0;
    $dfwiki->listofteachers = isset($dfwiki->listofteachers) ? 1 : 0;
    $dfwiki->editorrows = isset($dfwiki->editorrows) ? $dfwiki->editorrows : 40;
    $dfwiki->editorcols = isset($dfwiki->editorcols) ? $dfwiki->editorcols : 60;
    $dfwiki->evaluation = isset($dfwiki->evaluation) ? $dfwiki->evaluation : 0;
    $dfwiki->notetype = isset($dfwiki->notetype) ? $dfwiki->notetype : 0;
    $wikimanager = wiki_manager_get_instance();
    //update page edit permission
    if ($dfwiki->effects == 'allpages') {
        $wikimanager->pages_set_editable($dfwiki->id, $dfwiki->editable);
    }
    # May have to add extra stuff in here #
    return $wikimanager->update_wiki($dfwiki);
}
function parse_internal_link($matches)
{
    global $WS, $CFG, $COURSE;
    // allow spaces before and after the internal link name
    $matches[1] = trim($matches[1]);
    $matches[1] = wiki_clean_name($matches[1]);
    $parts = explode('|', $matches[1]);
    $target = $parts[0];
    $label = $target;
    if (count($parts) > 1) {
        $label = $parts[1];
    }
    // internal links to sections
    $page = $target;
    $anchor = '';
    $anchortype = 0;
    $pos = strpos($target, '#');
    if ($pos > 0) {
        $page = substr($target, 0, $pos);
        if (substr($target, $pos + 1, 1) == "#") {
            // [[page##section]]: partial view of the section
            $anchortype = 2;
            $anchor = substr($target, $pos + 2, strlen($target) - $pos);
        } else {
            // [[page#section]]: page view plus scroll to section
            $anchortype = 1;
            $anchor = substr($target, $pos + 1, strlen($target) - $pos);
        }
    }
    // wikibook
    $wikibookname = '';
    if (count($parts) > 2 && preg_match("/^wikibook:(.*)/", $parts[2], $match)) {
        $wikibookname = $match[1];
    }
    $wikibookparam = '';
    if ($wikibookname) {
        $wikibookparam = '&wikibook=' . urlencode($wikibookname);
    }
    // we can have more than one section with the same name, so we link
    // to all of them in a list after the link.
    $res = '';
    $urls = wiki_view_page_url($page, $anchor, $anchortype, $wikibookname);
    $num_urls = count($urls);
    for ($i = 0; $i < $num_urls; $i++) {
        if ($i == 0) {
            // we render the first matched section as a normal link
            $res = '<a href="' . $urls[$i] . '">' . $label . '</a>';
        } elseif ($i == 1) {
            $res .= ' [<a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
        } else {
            $res .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
        }
        if ($i == $num_urls - 1 && $i > 0) {
            $res .= ']';
        }
    }
    if (!wiki_page_exists($WS, $page, true)) {
        $res = '<span class="nwikiwanted">' . $res . '</span>';
    } else {
        if ($anchor != '') {
            if (!wiki_section_exists($page, $anchor)) {
                $res = '<span class="nwikiwanted">' . $res . '</span>';
            }
        }
    }
    return $res;
}
function wiki_print_editor(&$content_text, $adddiscussion = '0', $addtitle = '', $oldcontent = '', &$WS)
{
    global $CFG, $USER;
    //show upload error @@@@@@@@
    //try to delete a file
    wiki_print_editor_setup($WS);
    require_once $CFG->dirroot . "/mod/wiki/weblib.php";
    require_once $CFG->dirroot . "/mod/wiki/locallib.php";
    //Scritp WIKI_TREE
    $prop = null;
    $prop->type = 'text/javascript';
    if (isset($WS->dfcourse)) {
        $prop->src = '../mod/wiki/editor/wiki_tree.js';
    } else {
        $prop->src = 'editor/wiki_tree.js';
    }
    wiki_script('', $prop);
    if (isset($WS->dfdir->error)) {
        foreach ($WS->dfdir->error as $err) {
            $prop = null;
            $prop->class = 'except';
            wiki_size_text($err, 3, $prop);
        }
    }
    if (substr($WS->pagedata->pagename, 0, strlen('discussion:')) != 'discussion:') {
        $event = 'view';
    } else {
        $event = 'discussion';
    }
    //mount editing form
    //Converts reserved chars for html to prevent chars misreading
    $pagetemp = stripslashes_safe($WS->pagedata->pagename);
    $pagetemp = wiki_clean_name($pagetemp);
    $prop = null;
    $prop->action = 'view.php?id=' . $WS->linkid . '&amp;page=' . $event . '/' . urlencode($pagetemp) . '&amp;gid=' . $WS->groupmember->groupid . '&amp;uid=' . $WS->member->id;
    $prop->method = 'post';
    $prop->enctype = 'multipart/form-data';
    $prop->id = 'prim';
    wiki_form_start($prop);
    wiki_table_start();
    $prop = null;
    $prop->name = 'dfformversion';
    $prop->value = $WS->pagedata->version ? $WS->pagedata->version : '0';
    wiki_input_hidden($prop);
    $prop = null;
    $prop->name = 'dfformcreated';
    $prop->value = $WS->pagedata->created;
    wiki_input_hidden($prop);
    $prop = null;
    $prop->name = 'dfformeditable';
    $prop->value = $WS->pagedata->editable;
    wiki_input_hidden($prop);
    $prop = null;
    if ($adddiscussion == '1') {
        $prop->value = 'adddiscussion';
    } else {
        $prop->value = 'edit';
    }
    $prop->name = 'dfformaction';
    wiki_input_hidden($prop);
    $prop = null;
    $prop->name = 'dfformeditor';
    $prop->value = $WS->pagedata->editor;
    wiki_input_hidden($prop);
    /* necessary for joining the section with the rest of the page */
    // section name
    $section = optional_param('section', '', PARAM_TEXT);
    $prop = null;
    $prop->name = 'section';
    $preview = optional_param('dfformpreview', '', PARAM_TEXT);
    if ($preview != '' || wiki_is_uploading_modified()) {
        // if come from a preview, upload file or delete file
        // the string is already unslashed and encoded
        $prop->value = $section;
    } else {
        // gotta strip the slashes and encode it
        $prop->value = urlencode(stripslashes($section));
    }
    wiki_input_hidden($prop);
    // section number
    $prop = null;
    $prop->name = 'sectionnum';
    $prop->value = optional_param('sectionnum', '', PARAM_INTEGER);
    wiki_input_hidden($prop);
    $prop = null;
    $prop->name = 'sectionhash';
    $prop->value = optional_param('sectionhash', '', PARAM_TEXT);
    wiki_input_hidden($prop);
    // discussion
    if ($adddiscussion == '1') {
        wiki_size_text(get_string('adddiscussionitem', 'wiki'), 2);
        print_string('title', 'wiki');
        wiki_br();
        $prop = null;
        $prop->name = 'dfformaddtitle';
        $prop->value = $addtitle;
        $prop->size = '80';
        wiki_input_text($prop);
        wiki_br(2);
        $prop = null;
        $prop->name = 'dfformoldcontent';
        $prop->value = $oldcontent;
        wiki_input_hidden($prop);
        wiki_hr();
        wiki_change_row();
        date_default_timezone_set('UTC');
        // add editing username and date to the end of each discussion (nadavkav patch)
        $txt = $content_text . '<br/><br/>  <i>(' . get_string('discussioneditby', 'wiki') . ': ' . $USER->firstname . " " . $USER->lastname . " " . get_string('discussionediton', 'wiki') . ": " . date(DATE_RFC822) . ")</i>";
    } else {
        $txt = $content_text;
    }
    // wikibook
    $prop = null;
    $prop->name = 'wikibook';
    $prop->value = optional_param('wikibook', '', PARAM_TEXT);
    wiki_input_hidden($prop);
    wiki_print_edit_area($txt, $WS);
    wiki_tags_print_editbox($WS);
    $prop = null;
    $prop->class = 'nwikileftnow';
    wiki_change_row($prop);
    if ($WS->upload_bar) {
        wiki_print_edit_uploaded($WS);
    }
    wiki_change_row();
    $prop = null;
    $prop->name = 'dfformsave';
    $prop->value = get_string('save', 'wiki');
    wiki_input_submit($prop);
    $prop = null;
    $prop->name = 'dfformpreview';
    $prop->value = get_string('preview');
    wiki_input_submit($prop);
    $prop = null;
    $prop->name = 'dfformcancel';
    $prop->value = get_string('cancel');
    wiki_input_submit($prop);
    $prop = null;
    $prop->name = 'dfformdefinitive';
    $prop->value = '1';
    $prop->id = 'checkbox';
    wiki_input_checkbox($prop);
    print_string('highlight', 'wiki');
    wiki_table_end();
    wiki_form_end();
}