<commit_before><?php

// create a WebID certificate + profile data
if (isset($_POST['SPKAC']) && isset($_POST['username'])) {
    // Prepare the request
    $name = (isset($_POST['name']))?$_POST['name']:'Anonymous';
    
    if (isset($_POST['path'])) {
        $_POST['path'] = (substr($_POST['path'], 0, 1) == '/')?substr($_POST['path'], 1):$_POST['path'];
        $path = $_POST['path'];
    } else {
        $path = 'profile/card#me';
    }
    // Exit if we don't have a #
    if (strpos($path, '#') === false) // missing # 
        die("You must at least provide a # fragment. For example: #me or #public.");

    // remove the # fragment so we get the profile document path
    $path_frag = explode('#', $path);
    $profile = $path_frag[0];
    $hash = $path_frag[1];
    $_root = $_ENV['CLOUD_DATA'].'/'.$_POST['username'].'.'.ROOT_DOMAIN;
    
    // rebuild path for the profile document
    $webid_file = $_root.'/'.$profile;

    // create but do not overwrite existing profile document
    if (file_exists($webid_file) === true) {
        die('Error: <strong>'. $path.'</strong> already exists!');
    } else {           
        // check if the root dir exists and create it (recursively) if it doesn't
        if (!mkdir(dirname($webid_file), 0755, true))
            die('Cannot create directory at '.dirname($webid_file).', please check permissions.');
    }

    $BASE = 'http://'.$_POST['username'].'.'.$_SERVER['SERVER_NAME']; // force https
    $email = isset($_POST['email'])?$_POST['email']:null;
    $spkac = str_replace(str_split("\n\r"), '', $_POST['SPKAC']);
    $webid = $BASE.'/'.$path;
    
    // --- Cert ---
    $cert_cmd = 'python '.$_ENV['CLOUD_HOME'].'/py/pki.py '.
                    " -s '$spkac'" .
                    " -n '$name'" .
                    " -w '$webid'";
    
    // Send the certificate back to the user
    header('Content-Type: application/x-x509-user-cert');
    
    $cert = trim(shell_exec($cert_cmd));
    $ret_cmd = "echo '$cert' | openssl x509 -in /dev/stdin -outform der";
    echo trim(shell_exec($ret_cmd));
    
    $mod_cmd = "echo '$cert' | openssl x509 -in /dev/stdin -modulus -noout";
    // remove the Modulus= part
    $output = explode('=', trim(shell_exec($mod_cmd)));
    $modulus = $output[1];
    
    // --- Workspaces ---
    // create master workspace
    $mw = 'ws/';
    $mw_file = $_root.'/'.$mw;
    $mw_uri = $BASE.'/'.$mw;
    if (!mkdir($_root.'/'.$mw, 0755, true))
            die('Cannot create workspace "'.$_root.'/'.$mw.'", please check permissions');
    // create dedicated workspaces
    $ws = array('apps', 'public', 'shared', 'private');
    foreach ($ws as $w) {
        $w = $_root.'/'.$mw.$w;
        if (!mkdir($w, 0755, true))
            die('Cannot create workspace "'.$w.'", please check permissions');
    }
    $ap_uri = $mw_uri.'apps/';
    $ap_file = $mw_file.'apps/';
    $sh_uri = $mw_uri.'shared/';
    $sh_file = $mw_file.'shared/';
    $pu_uri = $mw_uri.'public/';
    $sh_file = $mw_file.'public/';
    $pr_uri = $mw_uri.'private/';
    $pr_file =$mw_file.'private/';
    // end workspaces

    // --- Profile --- 
    
    // Write the new profile to disk
    $document = new Graph('', $webid_file, '', $BASE.'/'.$profile);
    if (!$document) {
        echo "Cannot create a new graph!";
        exit;
    }
    
    // add a PrimaryTopic
    $document->append_objects($BASE.'/'.$profile,
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
             array(array('type'=>'uri', 'value'=>'http://xmlns.com/foaf/0.1/PersonalProfileDocument')));
    $document->append_objects($BASE.'/'.$profile,
            'http://xmlns.com/foaf/0.1/primaryTopic',
             array(array('type'=>'uri', 'value'=>$webid)));
     
    // add a foaf:Person
    $document->append_objects($webid,
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
            array(array('type'=>'uri', 'value'=>'http://xmlns.com/foaf/0.1/Person')));
    // add name
    $document->append_objects($webid,
            'http://xmlns.com/foaf/0.1/name',
            array(array('type'=>'literal', 'value'=>$name)));
    // add mbox if we have one
    if (strlen($email) > 0) {
        $document->append_objects($webid,
                'http://xmlns.com/foaf/0.1/mbox',
                array(array('type'=>'uri', 'value'=>'mailto:'.$email)));
    }

    // ---- Add workspaces ----
    // add master workspace
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#masterWorkspace',
            array(array('type'=>'uri', 'value'=>$mw_uri)));
    // add apps workspace
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#workspace',
            array(array('type'=>'uri', 'value'=>$ap_uri)));
    // add public
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#workspace',
            array(array('type'=>'uri', 'value'=>$pu_uri)));
    // add shared
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#workspace',
            array(array('type'=>'uri', 'value'=>$sh_uri)));
    // add private
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#workspace',
            array(array('type'=>'uri', 'value'=>$pr_uri)));<commit_msg>Only create a shared space when creating a new WebID profile.<commit_after><?php

// create a WebID certificate + profile data
if (isset($_POST['SPKAC']) && isset($_POST['username'])) {
    // Prepare the request
    $name = (isset($_POST['name']))?$_POST['name']:'Anonymous';
    
    if (isset($_POST['path'])) {
        $_POST['path'] = (substr($_POST['path'], 0, 1) == '/')?substr($_POST['path'], 1):$_POST['path'];
        $path = $_POST['path'];
    } else {
        $path = 'profile/card#me';
    }
    // Exit if we don't have a #
    if (strpos($path, '#') === false) // missing # 
        die("You must at least provide a # fragment. For example: #me or #public.");

    // remove the # fragment so we get the profile document path
    $path_frag = explode('#', $path);
    $profile = $path_frag[0];
    $hash = $path_frag[1];
    $_root = $_ENV['CLOUD_DATA'].'/'.$_POST['username'].'.'.ROOT_DOMAIN;
    
    // rebuild path for the profile document
    $webid_file = $_root.'/'.$profile;

    // create but do not overwrite existing profile document
    if (file_exists($webid_file) === true) {
        die('Error: <strong>'. $path.'</strong> already exists!');
    } else {           
        // check if the root dir exists and create it (recursively) if it doesn't
        if (!mkdir(dirname($webid_file), 0755, true))
            die('Cannot create directory at '.dirname($webid_file).', please check permissions.');
    }

    $BASE = 'https://'.$_POST['username'].'.'.$_SERVER['SERVER_NAME']; // force https
    $email = isset($_POST['email'])?$_POST['email']:null;
    $spkac = str_replace(str_split("\n\r"), '', $_POST['SPKAC']);
    $webid = $BASE.'/'.$path;
    
    // --- Cert ---
    $cert_cmd = 'python '.$_ENV['CLOUD_HOME'].'/py/pki.py '.
                    " -s '$spkac'" .
                    " -n '$name'" .
                    " -w '$webid'";
    
    // Send the certificate back to the user
    header('Content-Type: application/x-x509-user-cert');
    
    $cert = trim(shell_exec($cert_cmd));
    $ret_cmd = "echo '$cert' | openssl x509 -in /dev/stdin -outform der";
    echo trim(shell_exec($ret_cmd));

    $mod_cmd = "echo '$cert' | openssl x509 -in /dev/stdin -modulus -noout";
    // remove the Modulus= part
    $output = explode('=', trim(shell_exec($mod_cmd)));
    $modulus = $output[1];
    
    // --- Workspaces ---
    // create shared storage space
    $storage_uri = $BASE.'/storage/';
    $storage_file = $_root.'/storage/';
    if (!mkdir($storage_file, 0755, true))
        die('Cannot create storage space "'.$storage_file.'", please check permissions');   
    // end workspaces

    // --- Profile --- 
    // Write the new profile to disk
    $document = new Graph('', $webid_file, '', $BASE.'/'.$profile);
    if (!$document) {
        echo "Cannot create a new graph!";
        exit;
    }
    
    // add a PrimaryTopic
    $document->append_objects($BASE.'/'.$profile,
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
             array(array('type'=>'uri', 'value'=>'http://xmlns.com/foaf/0.1/PersonalProfileDocument')));
    $document->append_objects($BASE.'/'.$profile,
            'http://xmlns.com/foaf/0.1/primaryTopic',
             array(array('type'=>'uri', 'value'=>$webid)));
     
    // add a foaf:Person
    $document->append_objects($webid,
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
            array(array('type'=>'uri', 'value'=>'http://xmlns.com/foaf/0.1/Person')));
    // add name
    $document->append_objects($webid,
            'http://xmlns.com/foaf/0.1/name',
            array(array('type'=>'literal', 'value'=>$name)));
    // add mbox if we have one
    if (strlen($email) > 0) {
        $document->append_objects($webid,
                'http://xmlns.com/foaf/0.1/mbox',
                array(array('type'=>'uri', 'value'=>'mailto:'.$email)));
    }

    // ---- Add workspaces ----
    // add shared storage space
    $document->append_objects($webid,
            'http://www.w3.org/ns/pim/space#storage',
            array(array('type'=>'uri', 'value'=>$storage_uri)));
{'fraction_non_alphanumeric': 0.20957254024091249, 'fraction_numerical': 0.012152222577550368, 'mean_word_length': 2.430347349177331, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 1, 'lorem ipsum': 0, 'www.': 10, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7280250', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3082, 'n_words': 618}