Location: PHPKode > projects > TinyCMS > inc/functions.php
<?php
/**************************
* TinyCMS.net
* TinyCMS - Simple, quick CMS
**************************/

// Get page
$curr_page = $_GET['page'];

// Generate page names
function pages(){
  if ($handle = opendir('tpl')) {
      while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
  			$file = substr($file, 0, -(strlen($file)-(strrpos($file, "."))));
  			if (($file == "wsl") || ($file == "home")){ } else {
                        echo "<a href=\"#$file\" class=\"active\">" . ucfirst($file) . "</a>";
               }
          }		
  
      }
      closedir($handle);
  }
}

function genNav(){
  if ($handle = opendir('tpl')) {
      while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
  			$file = substr($file, 0, -(strlen($file)-(strrpos($file, "."))));
  			if ($file == "wsl"){ } else {
                        echo "a[href=\"#$file\"],";
               }
          }		
  
      }
      closedir($handle);
  }
}

// Administrative functions
// Admin pages
function adminPages(){
  ////////////////
  global $url;
  ////////////////
  # TinyCMS version 1.3 addition -> Rename pages (new function)
  if ($_GET['rename'] != null){
    # Rename a page
    $title = $_GET['rename'];
    if ($title == "home"){
      echo "Sorry, you cannot rename the home-page.";
    } else {

      if (file_exists("tpl/$title.html")) {
        // File exists, continue with the rename
        if ($_GET['update'] != "1"){
          // Show little form
          echo "<h2 style='font-size:14px;'>Rename <i>$title</i></h2>";
          echo "
                 <form action='?view=admin&do=pages&rename=$title&update=1' method='post'>
                   New Title<br />
                   <input type='text' name='title' size='30'><br /><br />
                   <input type='submit' value='Update Page Name'>
                 </form>
               ";
          
        } else {
          
          if (trim($_POST['title']) == null){
            echo "Please don't leave the title field blank.";
          } else {
            
            $new_title = $_POST['title'];
            rename("tpl/$title.html","tpl/$new_title.html");
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=pages&renamed=1\">";
            exit();

          }
          
        }
        
      } else {
        echo "The file you're trying to rename doesn't exist.";
      }

    }
  } else {
    if ($_GET['delete'] != null){
      $title = $_GET['delete'];
      if ($title == "home"){
        echo "Sorry, you cannot delete the home page."; 
      } else {
        if (file_exists("tpl/$title.html")) {
          if ($_GET['confirm'] != "1"){
            echo "Are you sure you wish to delete <strong>$title</strong>? - <a href=\"?view=admin&do=pages&delete=$title&confirm=1\">Yes</a>"; } else {
              // Delete the file
              unlink("tpl/$title.html") or die("Error, couldn't delete the file, if you wish to delete this, delete it manually from the tpl folder.");
              echo "<div id=\"success\">Page deleted.</div>";
            }
        } else {
          echo "The page you're trying to delete doesn't exist.";
        }
      }
    } else {
      if ($_GET['create'] == "new"){
        if ($_GET['save'] == "1"){
          // Save new page 
          $title = $_POST['title'];
          $content = $_POST['page'];
          if (file_exists("tpl/$title.html")) {
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=pages&edit=$title&updated=3\">";
            exit();
          } else {
          $handle = @fopen("tpl/$title.html", "w") or die("error");
          fwrite($handle, $content);
          fclose($handle);
          echo "<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=pages&edit=$title&updated=2\">";
          exit();
          }
        } else {
          // Form
          echo "<form action=\"?view=admin&do=pages&create=new&save=1\" method=\"post\">";
          echo "<strong>Page Title</strong><br /><input type=\"text\" name=\"title\" size=\"50\"><br /><br />";
          echo "<textarea id=\"elm1\" name=\"page\"></textarea><br />";
          echo "<input type=\"submit\" value=\"Save Page\">";
          echo "</form>";
        }
      } else {
        if ($_GET['edit'] != null){
          if ($_GET['update'] != null){
            // Update page
            $file = "$_GET[edit]";
            $handle = @fopen("tpl/$file.html", "w") or exit("Error, the page ($file.html) doesn't exist, the script has stopped. (<a href='?view=admin'>Admin Home</a>)");
            $posted_content = trim($_POST['page']);
            if ($_POST['page'] == null){ exit("<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=pages&edit=$file&updated=0\">"); } else {
            fwrite($handle, $posted_content);
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=pages&edit=$file&updated=1\">";
            }
            fclose($handle);
          } else {
            $file = "$_GET[edit]";
            $handle = @fopen("tpl/$file.html", "r") or exit("Error, the page doesn't exist, the script has stopped. (<a href='?view=admin'>Admin Home</a>)");
            if ($_GET['updated'] == "1"){ echo "<div id=\"success\">Page successfully updated - <a href=\"../#$file\" target=\"_blank\">Check it out!</a></div>"; } elseif ($_GET['updated'] == "0"){ echo "<div id=\"error\">Page could not be updated</div>"; } elseif ($_GET['updated'] == "2"){ echo "<div id=\"success\">Page successfully created</div>";  } elseif ($_GET['updated'] == "3"){ echo "<div id=\"error\">Page already exists</div>"; }
            // Form
            echo "<form action=\"?view=admin&do=pages&edit=$file&update=$file\" method=\"post\">";
            echo "<textarea id=\"elm1\" name=\"page\">" . fread($handle, 100000) . "</textarea><br />";
            echo "<input type=\"submit\" value=\"Save Page\">";
            echo "</form>";
            fclose($handle);
          }
        } else {
          if ($_GET['renamed'] == "1"){
            echo "<div id=\"success\">Page successfully renamed.</div>";
          }
          echo "<div class=\"listing\"><a href=\"?view=admin&do=pages&edit=home\" class=\"active\">Home</a>
                <span style=\"float:right;\"><a href=\"?view=admin&do=pages&edit=home\"><img src=\"$url/admin/ico/edit.png\"></a></span>
                </div>";
          if ($handle = opendir('tpl')) {
              while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
          			$file = substr($file, 0, -(strlen($file)-(strrpos($file, "."))));
          			if (($file == "wsl") || ($file == "home")){ } else {
                                echo "<div class=\"listing\"><a href=\"?view=admin&do=pages&edit=$file\" class=\"active\">" . ucfirst($file) . "</a>
                                      <span style=\"float:right;\">
                                        <a href=\"?view=admin&do=pages&edit=$file\"><img src=\"$url/admin/ico/edit.png\"></a>
                                        <a href=\"?view=admin&do=pages&rename=$file\"><img src=\"$url/admin/ico/rename.png\"></a>
                                        <a href=\"?view=admin&do=pages&delete=$file\"><img src=\"$url/admin/ico/delete.png\"></a>
                                      </span>
                                      </div>
                                     ";
                       }
                  }		
          
              }
              closedir($handle);
          }
        }
      }
    }
  }
}

// Function to modify the settings file
function showSettings(){
  if ($_GET['update'] == "1"){
    $handle = @fopen("inc/custom_conf.php", "w") or exit("Error loading custom configuration, the script has stopped. (<a href='?view=admin'>Admin Home</a>)");
    $posted_content = stripslashes($_POST[page]);
    @fwrite($handle, $posted_content) or die("Error");
    echo "<meta http-equiv=\"REFRESH\" content=\"0;url=?view=admin&do=settings&updated=1\">";
    fclose($handle);
  } else {
    if ($_GET['updated'] == "1"){ echo "<div id=\"success\">Custom configuration successfully updated</div>"; }
    $handle = @fopen("inc/custom_conf.php", "r") or exit("Error loading custom configuration, the script has stopped. (<a href='?view=admin'>Admin Home</a>)");
    // Form
    echo "<form action=\"?view=admin&do=settings&update=1\" method=\"post\">";
    echo "<textarea name=\"page\">" . fread($handle, 100000) . "</textarea><br />";
    echo "<input type=\"submit\" value=\"Save Settings\">";
    echo "</form>";
  }
}

?>
Return current item: TinyCMS