Skip to content
This repository
Browse code

Db-based codemirror mimes. Cleanup.

  • Loading branch information...
commit a92e8f3e0ad59785dbd603d8f49a3237fbd28265 1 parent bb3e441
Wim Tibackx WimTibackx authored
2  api.php
... ... @@ -1,5 +1,5 @@
1 1 <?php
2   -include "code.php";
  2 +include "inc/ecoder.php";
3 3
4 4 $controller=(isset($_GET['controller']) ? $_GET['controller'] : "");
5 5 $action=(isset($_GET['action']) ? $_GET['action'] : "");
29 code/base/server.php
... ... @@ -1,29 +0,0 @@
1   -<?php
2   -session_start();
3   -$gm_server_http_host=strip_tags(htmlentities($_SERVER['HTTP_HOST']));
4   -$live=true;
5   -$code=array("domain_cookie"=>""); //Fix notice. Crappy code(tm)
6   -
7   -if (strstr($gm_server_http_host,"localhost")==TRUE) {
8   - $live=false;
9   - ini_set( "session.cookie_domain", "" );
10   -
11   - $code['permissions_file'] = 0777;
12   - $code['permissions_dir'] = 0777;
13   -
14   - error_reporting(E_ALL);
15   - ini_set('display_errors', '1');
16   -} else {
17   - ini_set( "session.cookie_domain", $code['domain_cookie'] );
18   -
19   - $code['permissions_file'] = 0744;
20   - $code['permissions_dir'] = 0755;
21   -}
22   -
23   -setlocale(LC_ALL, 'gbr_GBR');
24   -
25   -$garbage_timeout = 3*60*60; // 3 hours -- garbage collection timeout ##
26   -ini_set( 'session.gc_maxlifetime', $garbage_timeout );
27   -
28   -$session_expire = 60*60*24*100; // 100 days - cookie expiration ##
29   -session_set_cookie_params( $session_expire );
16 code/ecoder.js
@@ -11,16 +11,14 @@ var ecoder=(function(){
11 11 $("body").on("contextmenu",false);
12 12 getInfo();
13 13 templ.load();
14   - $(document).on("ecoder.infoloaded",function() {
15   - trans.load();
16   - });
  14 + trans.load();
17 15
18 16 $(document).on("ecoder.transtemplready",function() {
19 17 initTabs();
20 18 openSplash();
21 19 });
22 20
23   - delete status;
  21 + //delete status;
24 22 }
25 23
26 24 function openSplash() {
@@ -33,15 +31,7 @@ var ecoder=(function(){
33 31 }
34 32
35 33 function getInfo() {
36   - $.ajax({
37   - url:"api.php?controller=env",
38   - datatype:'json',
39   - success:function(json) {
40   - info=(JSON.parse(json));
41   - status.info=true;
42   - $(document).trigger("ecoder.infoloaded");
43   - }
44   - });
  34 + info=env;
45 35 }
46 36
47 37 function callAction(controller,action,data,fn) {
16 code/env.php
@@ -17,6 +17,22 @@ public static function get() {
17 17 $env->__toOutput();
18 18 }
19 19
  20 + public static function getNoOutput() {
  21 + $env=new Env();
  22 + return json_encode($env->__toStdObj());
  23 + }
  24 +
  25 + public function __toStdObj() {
  26 + $s=new stdClass();
  27 + $s->maxUploadSize=$this->maxUploadSize;
  28 + $s->dirSep=$this->dirSep;
  29 + $s->tree_showHidden=$this->tree_showHidden;
  30 + $s->lang=$this->lang;
  31 + $s->autosave=$this->autosave;
  32 + $s->version=$this->version;
  33 + return $s;
  34 + }
  35 +
20 36 public function __toOutput() {
21 37 Output::add("maxUploadSize",$this->maxUploadSize);
22 38 Output::add("dirSep",$this->dirSep);
5 code/filemanipulation.php
@@ -226,9 +226,8 @@ public static function upload() {
226 226 public static function getFileEditingInfo($file) {
227 227 $sfi=new SplFileInfo($file);
228 228 $content=trim(htmlspecialchars(file_get_contents($sfi->getRealPath())));
229   - $ext=$sfi->getExtension();
230   - $mimes=array("php"=>"application/x-httpd-php-open","js"=>"text/javascript","html"=>"text/html","css"=>"text/css","text"=>"text/plain","cpp"=>"text/x-c++src", "c"=>"text/x-csrc","py"=>"text/x-python","pl"=>"text/x-perl");
231   - $cmMime=$mimes[$ext];
  229 + $ext=pathinfo($sfi->getPathname(),PATHINFO_EXTENSION);
  230 + $cmMime=Extension::findFileTypeByExtension($ext)->getCmmime();
232 231 Output::add("content",$content);
233 232 Output::add("cmMime",$cmMime);
234 233 Output::add("isWritable",$sfi->isWritable());
22 code/tree.php
@@ -42,24 +42,18 @@ public static function init() {
42 42 }
43 43
44 44 private function _getPropertiesFromSfi(SplFileInfo $sfi) {
45   - //Dirty solution for the subtypes for now
46   - //TODO / TOFIX when we introduce better actions
47   - $extToSubtype=array("html"=>"html","script"=>"js","css"=>"css","text"=>array("txt","htaccess","ini"),"php"=>"php","cpp"=>"cpp","c"=>array("c","h"),"py"=>"py","pl"=>"pl");
48   -
49 45 $this->name=$sfi->getFilename();
50 46 $this->type=$sfi->getType();
51 47 $this->path=$sfi->getPath();
52 48 $this->pathname=$sfi->getPathname();
53   - //Could use $sfi->getExtension() but that's only
54   - //available from php 5.3.6
55   - $this->ext=pathinfo($sfi->getPathname(),PATHINFO_EXTENSION);
56   - foreach ($extToSubtype as $k=>$v) {
57   - if (is_array($v)) {
58   - if (!in_array($this->ext,$v)) { continue; }
59   - } else if ($this->ext!=$v) {
60   - continue;
61   - }
62   - $this->subtype=$k;
  49 + if ($sfi->isDir()) {
  50 + $this->ext=null;
  51 + $this->subtype=Filetype::getByName("folder");
  52 + } else {
  53 + //Could use $sfi->getExtension() but that's only
  54 + //available from php 5.3.6
  55 + $this->ext=pathinfo($sfi->getPathname(),PATHINFO_EXTENSION);
  56 + $this->subtype=Extension::findFileTypeByExtension($this->ext)->getName();
63 57 }
64 58 }
65 59
23 code.php → inc/config.php
... ... @@ -1,5 +1,16 @@
1 1 <?php
2   -include "code/base/server.php";
  2 +$gm_server_http_host=strip_tags(htmlentities($_SERVER['HTTP_HOST']));
  3 +$live=true;
  4 +if (strstr($gm_server_http_host,"localhost")==TRUE) {
  5 + $live=false;
  6 + $code['permissions_file'] = 0777;
  7 + $code['permissions_dir'] = 0777;
  8 + error_reporting(E_ALL);
  9 + ini_set('display_errors', '1');
  10 +} else {
  11 + $code['permissions_file'] = 0744;
  12 + $code['permissions_dir'] = 0755;
  13 +}
3 14
4 15 // MOST IMPORTANT SETTING -- path to editable files ##
5 16 $code['root'] = 'D:/dev/ecoder/testingdata/'; // full path, with trailing slash ##
@@ -15,6 +26,7 @@
15 26 $code['codemirror']="2.2";
16 27
17 28 // security settings ##
  29 +/*
18 30 $code['secure'] = 0; // 0 = not secured || 1 = secured, uses settings below ##
19 31 $code['secure_variable'] = 'login_security'; // if isset indicates login active ##
20 32 $code['secure_url'] = 'http://www.gmeditor.com/user/login/?pass=ecoder'; // full url to login area -- ecoder variable allows return link ##
@@ -29,12 +41,17 @@
29 41 if ( $code['secure_root'] == 1 && isset( $_SESSION['tree_root'] ) ) { // assign session to root variable ##
30 42 $code['root'] = $_SESSION['tree_root']; // passed full path to editable root ##
31 43 }
32   -}
  44 +}*/
33 45
34 46 $cnf['uploadWhitelist'] = array( "php", "js", "html", "css", "txt", "htaccess", "ini" );
35 47 $cnf['showHidden']=false; //Show hidden files in tree?
36 48
37   -include "code/base/io.php";
  49 +$db=array(); //DB Identifiers for Redbean. See http://www.redbeanphp.com/manual/compatible
  50 +$db["db"]="mysql:host=localhost;dbname=ecoder";
  51 +$db["user"]="root";
  52 +$db["pass"]="r00tPASS";
  53 +
  54 +
38 55 include "code/filemanipulation.php";
39 56 include "code/tree.php";
40 57 include "code/env.php";
17 inc/ecoder.php
... ... @@ -0,0 +1,17 @@
  1 +<?php
  2 +include_once "lib/db.php";
  3 +include_once "io.php";
  4 +include_once "extension.php";
  5 +include_once "filetype.php";
  6 +
  7 +include_once "config.php";
  8 +
  9 +class Ecoder {
  10 +
  11 + static function __init() {
  12 + global $db;
  13 + Db::setConnectionInfo($db['db'],$db['user'],$db['pass']);
  14 + }
  15 +}
  16 +
  17 +Ecoder::__init();
27 inc/extension.php
... ... @@ -0,0 +1,27 @@
  1 +<?php
  2 +class Extension {
  3 + const DEFAULTFILETYPE="text";
  4 + private $name,$filetype_id;
  5 + private $filetype;
  6 +
  7 + public function Extension($ext) {
  8 + if (empty($ext)) { return null; }
  9 + $data=Db::getRow("SELECT * FROM extension WHERE name=?",$ext);
  10 + if (empty($data)) { return null; }
  11 + $this->name=$data->name;
  12 + $this->filetype_id=$data->filetype_id;
  13 + $this->filetype=new Filetype($this->filetype_id);
  14 + }
  15 +
  16 + public function getName() { return $this->name; }
  17 + public function getFiletype() { return $this->filetype; }
  18 +
  19 + public static function findFileTypeByExtension($ext) {
  20 + $exob=new Extension($ext);
  21 + $name=$exob->getName();
  22 + if (empty($name)) {
  23 + return Filetype::getByName(self::DEFAULTFILETYPE);
  24 + }
  25 + return $exob->getFiletype();
  26 + }
  27 +}
19 inc/filetype.php
... ... @@ -0,0 +1,19 @@
  1 +<?php
  2 +class Filetype {
  3 + private $id,$name,$cmmime;
  4 +
  5 + public function Filetype($id) {
  6 + $data=Db::getRow("SELECT * FROM filetype WHERE id=?",$id);
  7 + $this->id=$id;
  8 + $this->name=$data->name;
  9 + $this->cmmime=$data->cmmime;
  10 + }
  11 +
  12 + public function getName() { return $this->name; }
  13 + public function getCmmime() { return $this->cmmime; }
  14 +
  15 + public static function getByName($name) {
  16 + $id=Db::getValue("SELECT id FROM filetype WHERE name=?",$name);
  17 + return new Filetype($id);
  18 + }
  19 +}
0  code/base/io.php → inc/io.php
File renamed without changes
4 index.php
... ... @@ -1,10 +1,12 @@
1   -<?php include "code.php"; ?>
  1 +<?php include "inc/ecoder.php"; ?>
2 2 <!DOCTYPE html>
3 3 <html>
4 4 <head>
5 5 <script src="http://code.jquery.com/jquery-<?php echo $code['jQuery']; ?>.min.js"></script>
6 6 <script src="code/base/extensions.js" type="text/javascript"></script>
7 7
  8 + <script>var env=<?php echo Env::getNoOutput(); ?>;</script>
  9 +
8 10 <link rel="stylesheet" href="plug/codemirror-2_2-min.css" />
9 11 <!--<link rel="stylesheet" href="plug/codemirror/lib/codemirror.css" />
10 12 <link rel="stylesheet" href="plug/codemirror/mode/diff/diff.css" />
161 lib/db.php
... ... @@ -0,0 +1,161 @@
  1 +<?php
  2 +/**
  3 + * Db
  4 + * A simple wrapper for PDO. Inspired by http://www.fractalizer.ru/frpost_120/php-pdo-wrapping-and-making-sweet/
  5 + */
  6 +class Db {
  7 + private static $_pdoObject = null;
  8 +
  9 + protected static $_fetchMode = PDO::FETCH_OBJ;
  10 + protected static $_connectionStr = null;
  11 + protected static $_driverOptions = array();
  12 +
  13 + private static $_username = null;
  14 + private static $_password = null;
  15 +
  16 + // Set connection information
  17 + public static function setConnectionInfo($connectionStr,$user=null,$pass=null) {
  18 + self::$_connectionStr = $connectionStr;
  19 + self::$_username = $user;
  20 + self::$_password = $pass;
  21 +
  22 + // Making the connection blank
  23 + // Will connect with provided info on next query execution
  24 + self::$_pdoObject = null;
  25 + }
  26 +
  27 + /**
  28 + * Execute a statement and returns number of effected rows
  29 + *
  30 + * Should be used for query which doesn't return resultset
  31 + *
  32 + * @param string $sql SQL statement
  33 + * @param array $params A single value or an array of values
  34 + * @return integer number of effected rows
  35 + */
  36 + public static function execute($sql, $params = array())
  37 + {
  38 + $statement = self::_query($sql, $params);
  39 + return $statement->rowCount();
  40 + }
  41 +
  42 + /**
  43 + * Execute a statement and returns a single value
  44 + *
  45 + * @param string $sql SQL statement
  46 + * @param array $params A single value or an array of values
  47 + * @return mixed
  48 + */
  49 + public static function getValue($sql, $params = array())
  50 + {
  51 + $statement = self::_query($sql, $params);
  52 + return $statement->fetchColumn(0);
  53 + }
  54 +
  55 + /**
  56 + * Execute a statement and returns the first row
  57 + *
  58 + * @param string $sql SQL statement
  59 + * @param array $params A single value or an array of values
  60 + * @return array A result row
  61 + */
  62 + public static function getRow($sql, $params = array())
  63 + {
  64 + $statement = self::_query($sql, $params);
  65 + return $statement->fetch(self::$_fetchMode);
  66 + }
  67 +
  68 + /**
  69 + * Execute a statement and returns row(s) as 2D array
  70 + *
  71 + * @param string $sql SQL statement
  72 + * @param array $params A single value or an array of values
  73 + * @return array Result rows
  74 + */
  75 + public static function getResult($sql, $params = array())
  76 + {
  77 + $statement = self::_query($sql, $params);
  78 + return $statement->fetchAll(self::$_fetchMode);
  79 + }
  80 +
  81 + public static function getLastInsertId($sequenceName = "")
  82 + {
  83 + return self::$_pdoObject->lastInsertId($sequenceName);
  84 + }
  85 +
  86 + public static function setFetchMode($fetchMode)
  87 + {
  88 + self::_connect();
  89 + self::$_fetchMode = $fetchMode;
  90 + }
  91 +
  92 + public static function getPDOObject()
  93 + {
  94 + self::_connect();
  95 + return self::$_pdoObject;
  96 + }
  97 +
  98 + public static function beginTransaction()
  99 + {
  100 + self::_connect();
  101 + self::$_pdoObject->beginTransaction();
  102 + }
  103 +
  104 + public static function commitTransaction()
  105 + {
  106 + self::$_pdoObject->commit();
  107 + }
  108 +
  109 + public static function rollbackTransaction()
  110 + {
  111 + self::$_pdoObject->rollBack();
  112 + }
  113 +
  114 + public static function setDriverOptions(array $options)
  115 + {
  116 + self::$_driverOptions = $options;
  117 + }
  118 +
  119 + private static function _connect()
  120 + {
  121 + if(self::$_pdoObject != null){
  122 + return;
  123 + }
  124 +
  125 + if(self::$_connectionStr == null) {
  126 + throw new PDOException('Connection information is empty. Use Db::setConnectionInfo to set them.');
  127 + }
  128 +
  129 + self::$_pdoObject = new PDO(self::$_connectionStr, self::$_username, self::$_password, self::$_driverOptions);
  130 + }
  131 +
  132 + /**
  133 + * Prepare and returns a PDOStatement
  134 + *
  135 + * @param string $sql SQL statement
  136 + * @param array $params Parameters. A single value or an array of values
  137 + * @return PDOStatement
  138 + */
  139 + private static function _query($sql, $params = array())
  140 + {
  141 + if(self::$_pdoObject == null) {
  142 + self::_connect();
  143 + }
  144 +
  145 + $statement = self::$_pdoObject->prepare($sql, self::$_driverOptions);
  146 +
  147 + if (! $statement) {
  148 + $errorInfo = self::$_pdoObject->errorInfo();
  149 + throw new PDOException("Database error [{$errorInfo[0]}]: {$errorInfo[2]}, driver error code is $errorInfo[1]");
  150 + }
  151 +
  152 + $paramsConverted = (is_array($params) ? ($params) : (array ($params )));
  153 +
  154 + if ((! $statement->execute($paramsConverted)) || ($statement->errorCode() != '00000')) {
  155 + $errorInfo = $statement->errorInfo();
  156 + throw new PDOException("Database error [{$errorInfo[0]}]: {$errorInfo[2]}, driver error code is $errorInfo[1]");
  157 + }
  158 +
  159 + return $statement;
  160 + }
  161 +}

0 comments on commit a92e8f3

Please sign in to comment.
Something went wrong with that request. Please try again.