-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodxclass.php
130 lines (117 loc) · 3.5 KB
/
modxclass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
defined( '_NE' ) or die( 'Restricted access' );
class DB {
const DB_TEMPLATE=1;
const DB_CONTENT=2;
const DB_CHUNK=3;
private $dbobj;
private $DB_cache;
private $DB_dbh;
private $DB_template;
private $DB_content;
//db configuration
private $hostname = "127.0.0.1";
private $username = "max";
private $password = "maxmax";
private $dbName = "max";
private $userstable = "modx_site_content";
private $templatetable = "modx_site_templates";
private $chunktable = "modx_site_htmlsnippets";
public function __construct(){
}
public final function __clone()
{
throw new BadMethodCallException("Clone is not allowed");
}
/**
* BaseConnect()
* Set or get connection to base
* @static
* @access private
* @return $DB_dbh
*/
private function BaseConnect() {
$this->DB_dbh = new PDO('mysql:host='.$this->hostname.';dbname='.$this->dbName ,$this->username,$this->password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->DB_cache = new XCache ();
}
/**
* BaseClose()
* Close connection
* @static
* @access private
* @return null
*/
private function BaseClose() {
//Close connection
$this->DB_dbh = NULL;
}
/**
* getPageFromAlias
*
* @param $uri
* @static
* @access public
* @return Get content from uri
*/
public function getPageFromAlias($uri)
{
$conn=$this->BaseConnect();
$content=$this->GetContent($uri, 1);
$template=$this->GetContent($content["template"], 0);
// -------------------------------------Make the output and ---Get chunks----------------------------------------
$out = preg_replace( "/\[\[\*content\]\]/", $content["content"], $template["content"]);
unset ($content);
preg_match_all("/\[\[[$]{0,}([a-zA-Z0-9_]{1,})([[?]{0,1}[a-zA-Z0-9_&=`\s]{0,}]{0,})\]\]/", $template["content"], $chunkarray, PREG_SET_ORDER);
// $chunkarray[0]<-ChunkString $chunkarray[1]<- ChunkName $chunkarray[3]<- Parametrs
foreach ($chunkarray as $chunk)
{
$content=$this->GetContent($chunk[1], 2);
$out = str_replace($chunk[0], $content["content"], $out);
};
$out = str_replace ("[[++site_url]]","http://localhost/",$out);
return $out;
}
private function GetContent ($key, $type){
switch ($type) {
case 0: //Get template
//$this->DB_dbh
$cache = $this->DB_cache->get($key);
if (!isset($cache))
{
$sth = $this->DB_dbh->prepare("SELECT content as 'content' FROM ".$this->templatetable." WHERE id= '".$key."' LIMIT 0 , 1");
$sth->execute();
$arr=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$this->DB_cache->set($key,serialize($arr));
}
else {$arr=unserialize($cache);}
break;
case 1: //Get content
$cache = $this->DB_cache->get($key);
if (!isset($cache))
{
$sth = $this->DB_dbh->prepare("SELECT id as 'id', pagetitle as 'title' , template as 'template', content as 'content' FROM ".$this->userstable." WHERE uri= '".$key."' LIMIT 0 , 1");
$sth->execute();
$arr = $sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$this->DB_cache->set($key,serialize($arr));
}
else {$arr=unserialize($cache);}
break;
case 2: //Get chunks
$cache = $this->DB_cache->get($key);
if (!isset($cache))
{
$sth = $this->DB_dbh->prepare("SELECT snippet as 'content' FROM ".$this->chunktable." WHERE name='".$key."' LIMIT 1");
$sth->execute();
$arr= $sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$this->DB_cache->set($key,serialize($arr));
}
else {$arr=unserialize($cache);}
break;
}
return $arr;
}
}
?>