<commit_before><?
include_once("sqllib.inc.php");
include_once("contentifier-admin.inc.php");

abstract class Contentifier
{
  // API FUNCTIONS
  public function sqlhost() { return "localhost"; }
  public function sqluser() { return "contentifier"; }
  public function sqldb() { return "contentifier"; }
  public function templatefile() { return "template.html"; }
  public function rewriteenabled() { return false; }
  abstract public function sqlpass();
  public function rooturl() { return $this->rootURL; }
  public function slug() { return $this->slug; }

  use ContentifierAdmin;  
  
  function escape($s)
  {
    return htmlspecialchars($s,ENT_QUOTES);
  }
  function buildurl($slug,$params=array())
  {
    if ($slug=="/") $slug = "";
    if ($this->rewriteenabled())
    {
      return $this->rooturl().($slug?$slug."/":"").($params?"?".http_build_query($params):"");
    }
    else
    {
      $params["page"] = $slug;
      return $this->rooturl()."?".http_build_query($params);
    }
  }
  function redirect($url)
  {
    header("Location: ".$url);
    exit();
  }
  function initurls()
  {
    $this->url = ($_SERVER["HTTPS"]=="on"?"https":"http").":/"."/".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
    
    $dir = dirname($_SERVER["PHP_SELF"]);

    $this->rootRelativeURL = $_SERVER['REQUEST_URI'];
    if (strlen($dir) > 1)
    {
      $this->rootRelativeURL = substr($this->rootRelativeURL,strlen($dir));
    }
    list($this->rootRelativePath,) = explode("?",$this->rootRelativeURL);
    $this->rootURL = substr($this->url,0,-strlen($this->rootRelativeURL))."/";
  }
  function extractslug()
  {
    $this->slug = "";
    if ($this->rewriteenabled())
    {
      $this->slug = trim($this->rootRelativePath,'/');
    }
    else
    {
      $this->slug = $_GET["page"];
    }
    if (!$this->slug)
    {
      $this->slug = "index";
    }
  }
  function menu()
  {
    $rows = $this->sql->selectRows($this->sql->escape("select * from menu order by `order`"));
    if ($rows)
    {
      $out = "<ul>";
      foreach($rows as $row)
      {
        $url = $row->url;
        if (strstr($url,":/"."/")===false)
        {
          $url = $this->buildurl(trim($url,"/"));
        }
        $out .= "<li>";
        $out .= "<a href='".$this->escape($url)."'>".$this->escape($row->label)."</a>";
        $out .= "</li>";
      }
      $out .= "</ul>";
    }
    return $out;
  }
  function content()
  {
    $row = $this->getpagebyslug($this->slug);
    if ($row)
    {
      $content = $row->content;
      switch($row->format)
      {
        case "text": $content = nl2br($this->escape($content)); break;
      }
      return $content;
    }
    else
    {
      return "<h1>404</h1><p>Page '".$this->escape($this->slug)."' not found</p>";
    }
  }
  function contenttokens()
  {
    return array(
      "{%MENU%}" => $this->menu(),
      "{%CONTENT%}" => $this->content(),
      "{%ROOTURL%}" => $this->rooturl(),
      "{%SLUG%}" => $this->slug(),
    );
  }
  function template()
  {
    return file_exists($this->templatefile()) ? file_get_contents($this->templatefile()) : "<!DOCTYPE html>\n<html><body><nav>{%MENU%}</nav>{%CONTENT%}</body></html>";
  }
  function render()
  {
    if ($this->slug == "admin")
    {
      $this->renderadmin();
      return;
    }
    $tokens = $this->contenttokens();
    $template = $this->template();
    $template = str_replace(array_keys($tokens),array_values($tokens),$template);
    echo $template;
  }
  public function getpagebyslug($slug)
  {
    return $this->sql->selectRow($this->sql->escape("select * from pages where slug='%s'",$slug));
  }
  public function install()
  {
    $init = array(
      "CREATE TABLE `menu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `order` int(11) NOT NULL DEFAULT '0', `label` varchar(128) NOT NULL, `url` varchar(256) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT;",
      "CREATE TABLE `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `slug` varchar(128) NOT NULL, `title` text NOT NULL, `content` text NOT NULL, `format` enum('text','html','wiki') NOT NULL DEFAULT 'text', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT;",
      "CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(64) NOT NULL, `password` varchar(128) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT;"
    );
    foreach($init as $v) $this->sql->Query($v);
  }
  public function run()
  {
    $this->sql = new SQLLib();
    $this->sql->Connect($this->sqlhost(),$this->sqluser(),$this->sqlpass(),$this->sqldb());
    $this->initurls();  
    $this->extractslug();
    $this->render();
  }
}
?><commit_msg>add install procedure<commit_after><?
include_once("sqllib.inc.php");
include_once("contentifier-admin.inc.php");

abstract class Contentifier
{
  // API FUNCTIONS
  public function sqlhost() { return "localhost"; }
  public function sqluser() { return "contentifier"; }
  public function sqldb() { return "contentifier"; }
  public function templatefile() { return "template.html"; }
  public function rewriteenabled() { return false; }
  abstract public function sqlpass();
  public function rooturl() { return $this->rootURL; }
  public function slug() { return $this->slug; }

  use ContentifierAdmin;  
  
  function escape($s)
  {
    return htmlspecialchars($s,ENT_QUOTES);
  }
  function buildurl($slug,$params=array())
  {
    if ($slug=="/") $slug = "";
    if ($this->rewriteenabled())
    {
      return $this->rooturl().($slug?$slug."/":"").($params?"?".http_build_query($params):"");
    }
    else
    {
      $params["page"] = $slug;
      return $this->rooturl()."?".http_build_query($params);
    }
  }
  function redirect($url)
  {
    header("Location: ".$url);
    exit();
  }
  function initurls()
  {
    $this->url = ($_SERVER["HTTPS"]=="on"?"https":"http").":/"."/".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
    
    $dir = dirname($_SERVER["PHP_SELF"]);

    $this->rootRelativeURL = $_SERVER['REQUEST_URI'];
    if (strlen($dir) > 1)
    {
      $this->rootRelativeURL = substr($this->rootRelativeURL,strlen($dir));
    }
    list($this->rootRelativePath,) = explode("?",$this->rootRelativeURL);
    $this->rootURL = substr($this->url,0,-strlen($this->rootRelativeURL))."/";
  }
  function extractslug()
  {
    $this->slug = "";
    if ($this->rewriteenabled())
    {
      $this->slug = trim($this->rootRelativePath,'/');
    }
    else
    {
      $this->slug = $_GET["page"];
    }
    if (!$this->slug)
    {
      $this->slug = "index";
    }
  }
  function menu()
  {
    $rows = $this->sql->selectRows($this->sql->escape("select * from menu order by `order`"));
    if ($rows)
    {
      $out = "<ul>";
      foreach($rows as $row)
      {
        $url = $row->url;
        if (strstr($url,":/"."/")===false)
        {
          $url = $this->buildurl(trim($url,"/"));
        }
        $out .= "<li>";
        $out .= "<a href='".$this->escape($url)."'>".$this->escape($row->label)."</a>";
        $out .= "</li>";
      }
      $out .= "</ul>";
    }
    return $out;
  }
  function content()
  {
    $row = $this->getpagebyslug($this->slug);
    if ($row)
    {
      $content = $row->content;
      switch($row->format)
      {
        case "text": $content = nl2br($this->escape($content)); break;
      }
      return $content;
    }
    else
    {
      return "<h1>404</h1><p>Page '".$this->escape($this->slug)."' not found</p>";
    }
  }
  function contenttokens()
  {
    return array(
      "{%MENU%}" => $this->menu(),
      "{%CONTENT%}" => $this->content(),
      "{%ROOTURL%}" => $this->rooturl(),
      "{%SLUG%}" => $this->slug(),
    );
  }
  function template()
  {
    return file_exists($this->templatefile()) ? file_get_contents($this->templatefile()) : "<!DOCTYPE html>\n<html><body><nav>{%MENU%}</nav>{%CONTENT%}</body></html>";
  }
  function render()
  {
    if ($this->slug == "admin")
    {
      $this->renderadmin();
      return;
    }
    $tokens = $this->contenttokens();
    $template = $this->template();
    $template = str_replace(array_keys($tokens),array_values($tokens),$template);
    echo $template;
  }
  public function getpagebyslug($slug)
  {
    return $this->sql->selectRow($this->sql->escape("select * from pages where slug='%s'",$slug));
  }
  public function install()
  {
    $this->sql = new SQLLib();
    $this->sql->Connect($this->sqlhost(),$this->sqluser(),$this->sqlpass(),$this->sqldb());
    $output = "<!DOCTYPE html>\n<html>".
    "<head>".
    "<title>Contentifier Install</title>".
    "<style type=\"text/css\">".
    "*{margin:0;padding:0;}".
    "body {font-family:'segoe ui',sans-serif;background:#ccc;}".
    "a {color:black;}".
    "h1 {background:#444;color:white;border:0;border-bottom:1px solid #888;padding:5px;}".
    "input{display:block;width:100%;padding:5px;margin:5px 0px;}".
    "textarea{height:calc(100vh - 400px);}".
    "input.submit{width:90%;display:inline-block;}".
    "input.delete{width:10%;display:inline-block;}".
    "label{color:#888;font-size:60%;}".
    "label.radio{display:inline-block;margin-right:10px;vertical-align:sub;color:#444;font-size:80%;}".
    "label.radio input{display:inline;width:auto;}".
    "form{width:300px;margin:30px auto;}".
    "p{width:300px;margin:30px auto;}".
    "code{font-family;monospace;background:#eee;color:black;}".
    "footer, footer a{color:#999;margin:5px;font-size:10px;text-align:right;}".
    "</style>".
    "</head>".
    "<body>".
    "<h1>Contentifier Installation</h1>";
    if ($_POST["username"] && $_POST["password"])
    {
      $init = array(
        "CREATE TABLE `menu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `order` int(11) NOT NULL DEFAULT '0', `label` varchar(128) NOT NULL, `url` varchar(256) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;",
        "CREATE TABLE `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `slug` varchar(128) NOT NULL, `title` text NOT NULL, `content` text NOT NULL, `format` enum('text','html','wiki') NOT NULL DEFAULT 'text', PRIMARY KEY (`id`), UNIQUE KEY `slug` (`slug`)) ENGINE=InnoDB;",
        "CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(64) NOT NULL, `password` varchar(128) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`)) ENGINE=InnoDB;"
      );
      foreach($init as $v) $this->sql->Query($v);
      $this->sql->insertRow("users",array(
        "username" => $_POST["username"],
        "password" => password_hash($_POST["password"],PASSWORD_DEFAULT)
      ));
      $output .= "<p>Installation successful; now replace the <code>\$...-&gt;install()</code> in your main entry point".
      " file with <code>\$...-&gt;run()</code> and <a href='".$this->buildurl("admin")."'>load the admin to add some content</a>.</p>";
    }
    else
    {
      $output .=
      "<form method='post'>".
      "<h2>Create your first user</h2>".
      "<label>Username:</label>".
      "<input name='username' required='yes'/>".
      "<label>Password:</label>".
      "<input name='password' required='<PASSWORD>' type='password'/>".
      "<input type='hidden' name='token' value='".$this->escape($token)."'/>".
      "<input type='submit' name='createUser' value='Create'/>".
      "</form>";
    }
    $output .=
    "</body>".
    "</html>";
    echo $output;
  }
  public function run()
  {
    $this->sql = new SQLLib();
    $this->sql->Connect($this->sqlhost(),$this->sqluser(),$this->sqlpass(),$this->sqldb());
    $this->initurls();  
    $this->extractslug();
    $this->render();
  }
}
?>
{'fraction_non_alphanumeric': 0.22136074570955658, 'fraction_numerical': 0.010628103493335657, 'mean_word_length': 3.242424242424242, 'pattern_counts': {'":': 10, '<': 73, '<?xml version=': 0, '>': 222, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4796982', 'n_tokens_mistral': 4020, 'n_tokens_neox': 3952, 'n_words': 713}