database.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2010 - 2020
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Luis Daniel Lucio Quiroz <[email protected]>
  21. */
  22. include "root.php";
  23. //define the database class
  24. if (!class_exists('database')) {
  25. class database {
  26. /**
  27. * Define the class variables
  28. */
  29. public $db;
  30. public $driver;
  31. public $type;
  32. public $host;
  33. public $port;
  34. public $db_name;
  35. public $db_secure;
  36. public $db_cert_authority;
  37. public $username;
  38. public $password;
  39. public $path;
  40. public $table;
  41. public $where; //array
  42. public $order_by; //array
  43. public $order_type;
  44. public $limit;
  45. public $offset;
  46. public $fields;
  47. public $count;
  48. public $sql;
  49. public $result;
  50. public $app_name;
  51. public $app_uuid;
  52. public $domain_uuid;
  53. /**
  54. * Called when the object is created
  55. */
  56. public function __construct() {
  57. if (!isset($this->domain_uuid)) {
  58. $this->domain_uuid = $_SESSION['domain_uuid'];
  59. }
  60. }
  61. /**
  62. * Called when there are no references to a particular object
  63. * unset the variables used in the class
  64. */
  65. public function __destruct() {
  66. foreach ($this as $key => $value) {
  67. unset($this->$key);
  68. }
  69. }
  70. /**
  71. * Connect to the database
  72. */
  73. public function connect() {
  74. if (strlen($this->db_name) == 0) {
  75. //include config.php
  76. include "root.php";
  77. if (file_exists($_SERVER["PROJECT_ROOT"]."/resources/config.php")) {
  78. include $_SERVER["PROJECT_ROOT"]."/resources/config.php";
  79. } elseif (file_exists($_SERVER["PROJECT_ROOT"]."/resources/config.php")) {
  80. include $_SERVER["PROJECT_ROOT"]."/resources/config.php";
  81. } elseif (file_exists("/etc/fusionpbx/config.php")){
  82. //linux
  83. include "/etc/fusionpbx/config.php";
  84. } elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) {
  85. //bsd
  86. include "/usr/local/etc/fusionpbx/config.php";
  87. }
  88. //backwards compatibility
  89. if (isset($dbtype)) { $db_type = $dbtype; }
  90. if (isset($dbhost)) { $db_host = $dbhost; }
  91. if (isset($dbport)) { $db_port = $dbport; }
  92. if (isset($dbname)) { $db_name = $dbname; }
  93. if (isset($dbusername)) { $db_username = $dbusername; }
  94. if (isset($dbpassword)) { $db_password = $dbpassword; }
  95. if (isset($dbfilepath)) { $db_path = $db_file_path; }
  96. if (isset($dbfilename)) { $db_name = $dbfilename; }
  97. //set defaults
  98. if (!isset($this->driver) && isset($db_type)) { $this->driver = $db_type; }
  99. if (!isset($this->type) && isset($db_type)) { $this->type = $db_type; }
  100. if (!isset($this->host) && isset($db_host)) { $this->host = $db_host; }
  101. if (!isset($this->port) && isset($db_port)) { $this->port = $db_port; }
  102. if (!isset($this->db_name) && isset($db_name)) { $this->db_name = $db_name; }
  103. if (!isset($this->db_secure) && isset($db_secure)) {
  104. $this->db_secure = $db_secure;
  105. }
  106. else {
  107. $this->db_secure = false;
  108. }
  109. if (!isset($this->username) && isset($db_username)) { $this->username = $db_username; }
  110. if (!isset($this->password) && isset($db_password)) { $this->password = $db_password; }
  111. if (!isset($this->path) && isset($db_path)) { $this->path = $db_path; }
  112. }
  113. if (strlen($this->driver) == 0) {
  114. $this->driver = $this->type;
  115. }
  116. //sanitize the database name
  117. $this->db_name = preg_replace('#[^a-zA-Z0-9_\-\.]#', '', $this->db_name);
  118. if ($this->driver == "sqlite") {
  119. if (strlen($this->db_name) == 0) {
  120. $server_name = $_SERVER["SERVER_NAME"];
  121. $server_name = str_replace ("www.", "", $server_name);
  122. $db_name_short = $server_name;
  123. $this->db_name = $server_name.'.db';
  124. }
  125. else {
  126. $db_name_short = $this->db_name;
  127. }
  128. $this->path = realpath($this->path);
  129. if (file_exists($this->path.'/'.$this->db_name)) {
  130. //connect to the database
  131. $this->db = new PDO('sqlite:'.$this->path.'/'.$this->db_name); //sqlite 3
  132. //PRAGMA commands
  133. $this->db->query('PRAGMA foreign_keys = ON;');
  134. $this->db->query('PRAGMA journal_mode = wal;');
  135. //add additional functions to SQLite so that they are accessible inside SQL
  136. //bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
  137. $this->db->sqliteCreateFunction('md5', 'php_md5', 1);
  138. $this->db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
  139. $this->db->sqliteCreateFunction('now', 'php_now', 0);
  140. $this->db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
  141. $this->db->sqliteCreateFunction('strleft', 'php_left', 2);
  142. $this->db->sqliteCreateFunction('strright', 'php_right', 2);
  143. }
  144. else {
  145. echo "not found";
  146. }
  147. }
  148. if ($this->driver == "mysql") {
  149. try {
  150. //mysql pdo connection
  151. if (strlen($this->host) == 0 && strlen($this->port) == 0) {
  152. //if both host and port are empty use the unix socket
  153. $this->db = new PDO("mysql:host=$this->host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$this->db_name", $this->username, $this->password);
  154. }
  155. else {
  156. if (strlen($this->port) == 0) {
  157. //leave out port if it is empty
  158. $this->db = new PDO("mysql:host=$this->host;dbname=$this->db_name;", $this->username, $this->password, array(
  159. PDO::ATTR_ERRMODE,
  160. PDO::ERRMODE_EXCEPTION
  161. ));
  162. }
  163. else {
  164. $this->db = new PDO("mysql:host=$this->host;port=$this->port;dbname=$this->db_name;", $this->username, $this->password, array(
  165. PDO::ATTR_ERRMODE,
  166. PDO::ERRMODE_EXCEPTION
  167. ));
  168. }
  169. }
  170. }
  171. catch (PDOException $error) {
  172. print "error: " . $error->getMessage() . "<br/>";
  173. die();
  174. }
  175. }
  176. if ($this->driver == "pgsql") {
  177. //database connection
  178. try {
  179. if (strlen($this->host) > 0) {
  180. if (strlen($this->port) == 0) { $this->port = "5432"; }
  181. if ($this->db_secure == true) {
  182. $this->db = new PDO("pgsql:host=$this->host port=$this->port dbname=$this->db_name user=$this->username password=$this->password sslmode=verify-ca sslrootcert=$this->db_cert_authority");
  183. }
  184. else {
  185. $this->db = new PDO("pgsql:host=$this->host port=$this->port dbname=$this->db_name user=$this->username password=$this->password");
  186. }
  187. }
  188. else {
  189. $this->db = new PDO("pgsql:dbname=$this->db_name user=$this->username password=$this->password");
  190. }
  191. }
  192. catch (PDOException $error) {
  193. print "error: " . $error->getMessage() . "<br/>";
  194. die();
  195. }
  196. }
  197. if ($this->driver == "odbc") {
  198. //database connection
  199. try {
  200. $this->db = new PDO("odbc:".$this->db_name, $this->username, $this->password);
  201. }
  202. catch (PDOException $e) {
  203. echo 'Connection failed: ' . $e->getMessage();
  204. }
  205. }
  206. }
  207. public function tables() {
  208. //connect to the database if needed
  209. if (!$this->db) {
  210. $this->connect();
  211. }
  212. if ($this->type == "sqlite") {
  213. $sql = "SELECT name FROM sqlite_master ";
  214. $sql .= "WHERE type='table' ";
  215. $sql .= "order by name;";
  216. }
  217. if ($this->type == "pgsql") {
  218. $sql = "select table_name as name ";
  219. $sql .= "from information_schema.tables ";
  220. $sql .= "where table_schema='public' ";
  221. $sql .= "and table_type='BASE TABLE' ";
  222. $sql .= "order by table_name ";
  223. }
  224. if ($this->type == "mysql") {
  225. $sql = "show tables";
  226. }
  227. if ($this->type == "mssql") {
  228. $sql = "SELECT * FROM sys.Tables order by name asc";
  229. }
  230. $prep_statement = $this->db->prepare(check_sql($sql));
  231. $prep_statement->execute();
  232. $tmp = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  233. if ($this->type == "pgsql" || $this->type == "sqlite" || $this->type == "mssql") {
  234. if (is_array($tmp)) {
  235. foreach ($tmp as &$row) {
  236. $result[]['name'] = $row['name'];
  237. }
  238. }
  239. }
  240. if ($this->type == "mysql") {
  241. if (is_array($tmp)) {
  242. foreach ($tmp as &$row) {
  243. $table_array = array_values($row);
  244. $result[]['name'] = $table_array[0];
  245. }
  246. }
  247. }
  248. return $result;
  249. }
  250. public function table_info() {
  251. //public $db;
  252. //public $type;
  253. //public $table;
  254. //public $name;
  255. //connect to the database if needed
  256. if (!$this->db) {
  257. $this->connect();
  258. }
  259. //sanitize the names
  260. $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
  261. $this->db_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->db_name);
  262. //get the table info
  263. if (strlen($this->table) == 0) { return false; }
  264. if ($this->type == "sqlite") {
  265. $sql = "PRAGMA table_info(".$this->table.");";
  266. }
  267. if ($this->type == "pgsql") {
  268. $sql = "SELECT ordinal_position, ";
  269. $sql .= "column_name, ";
  270. $sql .= "data_type, ";
  271. $sql .= "column_default, ";
  272. $sql .= "is_nullable, ";
  273. $sql .= "character_maximum_length, ";
  274. $sql .= "numeric_precision ";
  275. $sql .= "FROM information_schema.columns ";
  276. $sql .= "WHERE table_name = '".$this->table."' ";
  277. $sql .= "and table_catalog = '".$this->db_name."' ";
  278. $sql .= "ORDER BY ordinal_position; ";
  279. }
  280. if ($this->type == "mysql") {
  281. $sql = "DESCRIBE ".$this->table.";";
  282. }
  283. if ($this->type == "mssql") {
  284. $sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '".$this->table."'";
  285. }
  286. $prep_statement = $this->db->prepare($sql);
  287. $prep_statement->execute();
  288. //set the result array
  289. return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  290. }
  291. public function fields() {
  292. //public $db;
  293. //public $type;
  294. //public $table;
  295. //public $name;
  296. //get the table info
  297. $table_info = $this->table_info();
  298. //set the list of fields
  299. if ($this->type == "sqlite") {
  300. if (is_array($table_info)) {
  301. foreach($table_info as $row) {
  302. $result[]['name'] = $row['name'];
  303. }
  304. }
  305. }
  306. if ($this->type == "pgsql") {
  307. if (is_array($table_info)) {
  308. foreach($table_info as $row) {
  309. $result[]['name'] = $row['column_name'];
  310. }
  311. }
  312. }
  313. if ($this->type == "mysql") {
  314. if (is_array($table_info)) {
  315. foreach($table_info as $row) {
  316. $result[]['name'] = $row['Field'];
  317. }
  318. }
  319. }
  320. if ($this->type == "mssql") {
  321. if (is_array($table_info)) {
  322. foreach($table_info as $row) {
  323. $result[]['name'] = $row['COLUMN_NAME'];
  324. }
  325. }
  326. }
  327. //return the result array
  328. return $result;
  329. }
  330. //public function disconnect() {
  331. // return null;
  332. //}
  333. public function find() {
  334. //connect;
  335. //table;
  336. //where;
  337. //order_by;
  338. //limit;
  339. //offset;
  340. //connect to the database if needed
  341. if (!$this->db) {
  342. $this->connect();
  343. }
  344. //sanitize the name
  345. $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
  346. //get data from the database
  347. $sql = "select * from ".$this->table." ";
  348. if ($this->where) {
  349. $i = 0;
  350. if (is_array($this->where)) {
  351. foreach($this->where as $row) {
  352. //sanitize the name
  353. $array['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['name']);
  354. //validate the operator
  355. switch ($row['operator']) {
  356. case "<": break;
  357. case ">": break;
  358. case "<=": break;
  359. case ">=": break;
  360. case "=": break;
  361. case ">=": break;
  362. case "<>": break;
  363. case "!=": break;
  364. default:
  365. //invalid operator
  366. return false;
  367. }
  368. //build the sql
  369. if ($i == 0) {
  370. //$sql .= 'where '.$row['name']." ".$row['operator']." '".$row['value']."' ";
  371. $sql .= 'where '.$row['name']." ".$row['operator']." :".$row['name']." ";
  372. }
  373. else {
  374. //$sql .= "and ".$row['name']." ".$row['operator']." '".$row['value']."' ";
  375. $sql .= "and ".$row['name']." ".$row['operator']." :".$row['name']." ";
  376. }
  377. //add the name and value to the params array
  378. $params[$row['name']] = $row['value'];
  379. //increment $i
  380. $i++;
  381. }
  382. }
  383. }
  384. if (is_array($this->order_by)) {
  385. $sql .= "order by ";
  386. $i = 1;
  387. if (is_array($this->order_by)) {
  388. foreach($this->order_by as $row) {
  389. //sanitize the name
  390. $row['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $row['name']);
  391. //sanitize the order
  392. switch ($row['order']) {
  393. case "asc":
  394. break;
  395. case "desc":
  396. break;
  397. default:
  398. $row['order'] = '';
  399. }
  400. //build the sql
  401. if (count($this->order_by) == $i) {
  402. $sql .= $row['name']." ".$row['order']." ";
  403. }
  404. else {
  405. $sql .= $row['name']." ".$row['order'].", ";
  406. }
  407. //increment $i
  408. $i++;
  409. }
  410. }
  411. }
  412. //limit
  413. if (isset($this->limit) && is_numeric($this->limit)) {
  414. $sql .= "limit ".$this->limit." ";
  415. }
  416. //offset
  417. if (isset($this->offset) && is_numeric($this->offset)) {
  418. $sql .= "offset ".$this->offset." ";
  419. }
  420. $prep_statement = $this->db->prepare($sql);
  421. if ($prep_statement) {
  422. $prep_statement->execute($params);
  423. $array = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  424. unset($prep_statement);
  425. return $array;
  426. }
  427. else {
  428. return false;
  429. }
  430. }
  431. // Use this function to execute complex queries
  432. public function execute($sql, $parameters = null, $return_type = 'all') {
  433. //connect to the database if needed
  434. if (!$this->db) {
  435. $this->connect();
  436. }
  437. //set the error mode
  438. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  439. //execute the query, and return the results
  440. try {
  441. $prep_statement = $this->db->prepare($sql);
  442. if (is_array($parameters)) {
  443. $prep_statement->execute($parameters);
  444. }
  445. else {
  446. $prep_statement->execute();
  447. }
  448. $message["message"] = "OK";
  449. $message["code"] = "200";
  450. $message["sql"] = $sql;
  451. if (is_array($parameters)) {
  452. $message["parameters"] = $parameters;
  453. }
  454. $this->message = $message;
  455. //return the results
  456. switch($return_type) {
  457. case 'all':
  458. return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  459. case 'row':
  460. return $prep_statement->fetch(PDO::FETCH_ASSOC);
  461. case 'column';
  462. return $prep_statement->fetchColumn();
  463. default:
  464. return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  465. }
  466. }
  467. catch(PDOException $e) {
  468. $message["message"] = "Bad Request";
  469. $message["code"] = "400";
  470. $message["error"]["message"] = $e->getMessage();
  471. if ($this->debug["sql"]) {
  472. $message["sql"] = $sql;
  473. }
  474. if (is_array($parameters)) {
  475. $message["parameters"] = $parameters;
  476. }
  477. $this->message = $message;
  478. return false;
  479. }
  480. }
  481. public function add() {
  482. //connect to the database if needed
  483. if (!$this->db) {
  484. $this->connect();
  485. }
  486. //sanitize the table name
  487. $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
  488. //count the fields
  489. $field_count = count($this->fields);
  490. //add data to the database
  491. $sql = "insert into ".$this->table;
  492. $sql .= " (";
  493. $i = 1;
  494. if (is_array($this->fields)) {
  495. foreach($this->fields as $name => $value) {
  496. $name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $name);
  497. if (count($this->fields) == $i) {
  498. $sql .= $name." \n";
  499. }
  500. else {
  501. $sql .= $name.", \n";
  502. }
  503. $i++;
  504. }
  505. }
  506. $sql .= ") \n";
  507. $sql .= "values \n";
  508. $sql .= "(\n";
  509. $i = 1;
  510. if (is_array($this->fields)) {
  511. foreach($this->fields as $name => $value) {
  512. $name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $name);
  513. if ($field_count == $i) {
  514. if (strlen($value) > 0) {
  515. //$sql .= "'".$value."' ";
  516. $sql .= ":".$name." \n";
  517. $params[$name] = trim($value);
  518. }
  519. else {
  520. $sql .= "null \n";
  521. }
  522. }
  523. else {
  524. if (strlen($value) > 0) {
  525. //$sql .= "'".$value."', ";
  526. $sql .= ":".$name.", \n";
  527. $params[$name] = trim($value);
  528. }
  529. else {
  530. $sql .= "null, \n";
  531. }
  532. }
  533. $i++;
  534. }
  535. }
  536. $sql .= ")\n";
  537. //execute the query, show exceptions
  538. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  539. try {
  540. //$this->sql = $sql;
  541. //$this->db->exec($sql);
  542. $prep_statement = $this->db->prepare($sql);
  543. $prep_statement->execute($params);
  544. }
  545. catch(PDOException $e) {
  546. echo "<b>Error:</b><br />\n";
  547. echo "<table>\n";
  548. echo "<tr>\n";
  549. echo "<td>\n";
  550. echo $e->getMessage();
  551. echo "</td>\n";
  552. echo "</tr>\n";
  553. echo "</table>\n";
  554. }
  555. unset($sql, $prep_statement, $this->fields);
  556. }
  557. public function update() {
  558. //connect to the database if needed
  559. if (!$this->db) {
  560. $this->connect();
  561. }
  562. //sanitize the table name
  563. $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
  564. //udate the database
  565. $sql = "update ".$this->table." set ";
  566. $i = 1;
  567. if (is_array($this->fields)) {
  568. foreach($this->fields as $name => $value) {
  569. $name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $name);
  570. if (count($this->fields) == $i) {
  571. if (strlen($name) > 0 && $value == null) {
  572. $sql .= $name." = null ";
  573. }
  574. else {
  575. //$sql .= $name." = '".$value."' ";
  576. $sql .= $name." = :".$name." ";
  577. $params[$name] = trim($value);
  578. }
  579. }
  580. else {
  581. if (strlen($name) > 0 && $value == null) {
  582. $sql .= $name." = null, ";
  583. }
  584. else {
  585. //$sql .= $name." = '".$value."', ";
  586. $sql .= $name." = :".$name.", ";
  587. $params[$name] = trim($value);
  588. }
  589. }
  590. $i++;
  591. }
  592. }
  593. $i = 0;
  594. if (is_array($this->where)) {
  595. foreach($this->where as $row) {
  596. //sanitize the name
  597. $row['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $row['name']);
  598. //validate the operator
  599. switch ($row['operator']) {
  600. case "<": break;
  601. case ">": break;
  602. case "<=": break;
  603. case ">=": break;
  604. case "=": break;
  605. case ">=": break;
  606. case "<>": break;
  607. case "!=": break;
  608. default:
  609. //invalid operator
  610. return false;
  611. }
  612. //build the sql
  613. if ($i == 0) {
  614. //$sql .= $row['name']." ".$row['operator']." '".$row['value']."' ";
  615. $sql .= "where ".$row['name']." ".$row['operator']." :".$row['name']." ";
  616. }
  617. else {
  618. //$sql .= $row['name']." ".$row['operator']." '".$row['value']."' ";
  619. $sql .= "and ".$row['name']." ".$row['operator']." :".$row['name']." ";
  620. }
  621. //add the name and value to the params array
  622. $params[$row['name']] = $row['value'];
  623. //increment $i
  624. $i++;
  625. }
  626. }
  627. //$this->db->exec(check_sql($sql));
  628. $prep_statement = $this->db->prepare($sql);
  629. $prep_statement->execute($params);
  630. unset($prep_statement);
  631. unset($this->fields);
  632. unset($this->where);
  633. unset($sql);
  634. }
  635. public function delete($delete_array) {
  636. //connect to the database if needed
  637. if (!$this->db) {
  638. $this->connect();
  639. }
  640. //set the message id
  641. $m = 0;
  642. //set the app name
  643. if (!isset($this->app_name)) {
  644. $this->app_name = $this->name;
  645. }
  646. //set the table prefix
  647. $table_prefix = 'v_';
  648. //debug sql
  649. $this->debug["sql"] = true;
  650. //debug info
  651. //echo "<pre>\n";
  652. //print_r($delete_array);
  653. //echo "</pre>\n";
  654. //exit;
  655. //get the current data
  656. if (is_array($delete_array)) {
  657. foreach($delete_array as $table_name => $rows) {
  658. foreach($rows as $row) {
  659. $i = 0;
  660. $sql = "select * from ".$table_prefix.$table_name." ";
  661. foreach($row as $field_name => $field_value) {
  662. if ($i == 0) { $sql .= "where "; } else { $sql .= "and "; }
  663. $sql .= $field_name." = :".$field_name." ";
  664. $parameters[$field_name] = $field_value;
  665. $i++;
  666. }
  667. if (strlen($field_value) > 0) {
  668. $results = $this->execute($sql, $parameters, 'all');
  669. if (is_array($results)) {
  670. $array[$table_name] = $results;
  671. }
  672. }
  673. unset($parameters);
  674. }
  675. }
  676. }
  677. //save the array
  678. $old_array = &$array;
  679. //start the atomic transaction
  680. $this->db->beginTransaction();
  681. //delete the current data
  682. if (is_array($delete_array)) {
  683. foreach($delete_array as $table_name => $rows) {
  684. //echo "table: ".$table_name."\n";
  685. foreach($rows as $row) {
  686. if (permission_exists($this->singular($table_name).'_delete')) {
  687. $sql = "delete from ".$table_prefix.$table_name." ";
  688. $i = 0;
  689. foreach($row as $field_name => $field_value) {
  690. //echo "field: ".$field_name." = ".$field_value."\n";
  691. if ($i == 0) { $sql .= "where "; } else { $sql .= "and "; }
  692. $sql .= $field_name." = :".$field_name." ";
  693. $parameters[$field_name] = $field_value;
  694. $i++;
  695. }
  696. try {
  697. $this->execute($sql, $parameters);
  698. $message["message"] = "OK";
  699. $message["code"] = "200";
  700. $message["uuid"] = $id;
  701. $message["details"][$m]["name"] = $this->name;
  702. $message["details"][$m]["message"] = "OK";
  703. $message["details"][$m]["code"] = "200";
  704. //$message["details"][$m]["uuid"] = $parent_key_value;
  705. if ($this->debug["sql"]) {
  706. $message["details"][$m]["sql"] = $sql;
  707. }
  708. $this->message = $message;
  709. $m++;
  710. unset($sql);
  711. unset($statement);
  712. }
  713. catch(PDOException $e) {
  714. $message["message"] = "Bad Request";
  715. $message["code"] = "400";
  716. $message["details"][$m]["name"] = $this->name;
  717. $message["details"][$m]["message"] = $e->getMessage();
  718. $message["details"][$m]["code"] = "400";
  719. if ($this->debug["sql"]) {
  720. $message["details"][$m]["sql"] = $sql;
  721. }
  722. $this->message = $message;
  723. $m++;
  724. }
  725. unset($parameters);
  726. } //if permission
  727. } //foreach rows
  728. } //foreach $array
  729. }
  730. //commit the atomic transaction
  731. $this->db->commit();
  732. //set the action if not set
  733. $transaction_type = 'delete';
  734. //get the UUIDs
  735. $user_uuid = $_SESSION['user_uuid'];
  736. //log the transaction results
  737. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/database_transactions/app_config.php")) {
  738. $sql = "insert into v_database_transactions ";
  739. $sql .= "(";
  740. $sql .= "database_transaction_uuid, ";
  741. if (isset($this->domain_uuid) && is_uuid($this->domain_uuid)) {
  742. $sql .= "domain_uuid, ";
  743. }
  744. if (isset($user_uuid) && is_uuid($user_uuid)) {
  745. $sql .= "user_uuid, ";
  746. }
  747. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  748. $sql .= "app_uuid, ";
  749. }
  750. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  751. $sql .= "app_name, ";
  752. }
  753. $sql .= "transaction_code, ";
  754. $sql .= "transaction_address, ";
  755. $sql .= "transaction_type, ";
  756. $sql .= "transaction_date, ";
  757. $sql .= "transaction_old, ";
  758. $sql .= "transaction_new, ";
  759. $sql .= "transaction_result ";
  760. $sql .= ")";
  761. $sql .= "values ";
  762. $sql .= "(";
  763. $sql .= "'".uuid()."', ";
  764. if (isset($this->domain_uuid) && is_uuid($this->domain_uuid)) {
  765. $sql .= "'".$this->domain_uuid."', ";
  766. }
  767. if (isset($user_uuid) && is_uuid($user_uuid)) {
  768. $sql .= ":user_uuid, ";
  769. }
  770. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  771. $sql .= ":app_uuid, ";
  772. }
  773. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  774. $sql .= ":app_name, ";
  775. }
  776. $sql .= "'".$message["code"]."', ";
  777. $sql .= ":remote_address, ";
  778. $sql .= "'".$transaction_type."', ";
  779. $sql .= "now(), ";
  780. if (is_array($old_array)) {
  781. $sql .= ":transaction_old, ";
  782. }
  783. else {
  784. $sql .= "null, ";
  785. }
  786. if (is_array($new_array)) {
  787. $sql .= ":transaction_new, ";
  788. }
  789. else {
  790. $sql .= "null, ";
  791. }
  792. $sql .= ":transaction_result ";
  793. $sql .= ")";
  794. $statement = $this->db->prepare($sql);
  795. if (isset($user_uuid) && is_uuid($user_uuid)) {
  796. $statement->bindParam(':user_uuid', $user_uuid);
  797. }
  798. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  799. $statement->bindParam(':app_uuid', $this->app_uuid);
  800. }
  801. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  802. $statement->bindParam(':app_name', $this->app_name);
  803. }
  804. $statement->bindParam(':remote_address', $_SERVER['REMOTE_ADDR']);
  805. if (is_array($old_array)) {
  806. $statement->bindParam(':transaction_old', json_encode($old_array, JSON_PRETTY_PRINT));
  807. }
  808. if (is_array($new_array)) {
  809. $statement->bindParam(':transaction_new', json_encode($new_array, JSON_PRETTY_PRINT));
  810. }
  811. $statement->bindParam(':transaction_result', json_encode($this->message, JSON_PRETTY_PRINT));
  812. $statement->execute();
  813. unset($sql);
  814. }
  815. } //delete
  816. public function count() {
  817. //connect to the database if needed
  818. if (!$this->db) {
  819. $this->connect();
  820. }
  821. //sanitize the table name
  822. $this->table = preg_replace('#[^a-zA-Z0-9_\-]#', '', $this->table);
  823. //get the number of rows
  824. $sql = "select count(*) as num_rows from ".$this->table." ";
  825. if ($this->where) {
  826. $i = 0;
  827. if (is_array($this->where)) {
  828. foreach($this->where as $row) {
  829. //sanitize the name
  830. $row['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $row['name']);
  831. //validate the operator
  832. switch ($row['operator']) {
  833. case "<": break;
  834. case ">": break;
  835. case "<=": break;
  836. case ">=": break;
  837. case "=": break;
  838. case ">=": break;
  839. case "<>": break;
  840. case "!=": break;
  841. default:
  842. //invalid operator
  843. return false;
  844. }
  845. //build the sql
  846. if ($i == 0) {
  847. //$sql .= $row['name']." ".$row['operator']." '".$row['value']."' ";
  848. $sql .= "where ".$row['name']." ".$row['operator']." :".$row['name']." ";
  849. }
  850. else {
  851. //$sql .= $row['name']." ".$row['operator']." '".$row['value']."' ";
  852. $sql .= "and ".$row['name']." ".$row['operator']." :".$row['name']." ";
  853. }
  854. //add the name and value to the params array
  855. $params[$row['name']] = $row['value'];
  856. //increment $i
  857. $i++;
  858. }
  859. }
  860. }
  861. unset($this->where);
  862. $prep_statement = $this->db->prepare($sql);
  863. if ($prep_statement) {
  864. $prep_statement->execute($params);
  865. $row = $prep_statement->fetch(PDO::FETCH_ASSOC);
  866. if ($row['num_rows'] > 0) {
  867. return $row['num_rows'];
  868. }
  869. else {
  870. return 0;
  871. }
  872. }
  873. unset($prep_statement);
  874. } //count
  875. public function select($sql, $parameters = '', $return_type = 'all') {
  876. //connect to the database if needed
  877. if (!$this->db) {
  878. $this->connect();
  879. }
  880. //set the error mode
  881. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  882. //execute the query, and return the results
  883. try {
  884. $prep_statement = $this->db->prepare($sql);
  885. if (is_array($parameters)) {
  886. $prep_statement->execute($parameters);
  887. }
  888. else {
  889. $prep_statement->execute();
  890. }
  891. $message["message"] = "OK";
  892. $message["code"] = "200";
  893. $message["sql"] = $sql;
  894. if (is_array($parameters)) {
  895. $message["parameters"] = $parameters;
  896. }
  897. $this->message = $message;
  898. //return the results
  899. switch($return_type) {
  900. case 'all':
  901. return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  902. case 'row':
  903. return $prep_statement->fetch(PDO::FETCH_ASSOC);
  904. case 'column':
  905. return $prep_statement->fetchColumn();
  906. default:
  907. return $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  908. }
  909. }
  910. catch(PDOException $e) {
  911. $message["message"] = "Bad Request";
  912. $message["code"] = "400";
  913. $message["error"]["message"] = $e->getMessage();
  914. if ($this->debug["sql"]) {
  915. $message["sql"] = $sql;
  916. }
  917. if (is_array($parameters)) {
  918. $message["parameters"] = $parameters;
  919. }
  920. $this->message = $message;
  921. return false;
  922. }
  923. } //select
  924. public function find_new() {
  925. //connect to the database if needed
  926. if (!$this->db) {
  927. $this->connect();
  928. }
  929. //set the name
  930. if (isset($array['name'])) {
  931. $this->name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['name']);
  932. }
  933. //set the uuid
  934. if (isset($array['uuid']) and $this->is_uuid($array['uuid'])) {
  935. $this->uuid = $array['uuid'];
  936. }
  937. //build the query
  938. $sql = "SELECT * FROM v_".$this->name." ";
  939. if (isset($this->uuid)) {
  940. //get the specific uuid
  941. $sql .= "WHERE ".$this->singular($this->name)."_uuid = '".$this->uuid."' ";
  942. }
  943. else {
  944. //where
  945. $i = 0;
  946. if (is_array($array)) {
  947. foreach($array['where'] as $row) {
  948. //sanitize the name
  949. $array['name'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['name']);
  950. //validate the operator
  951. switch ($row['operator']) {
  952. case "<": break;
  953. case ">": break;
  954. case "<=": break;
  955. case ">=": break;
  956. case "=": break;
  957. case ">=": break;
  958. case "<>": break;
  959. case "!=": break;
  960. default:
  961. //invalid operator
  962. return false;
  963. }
  964. //build the sql
  965. if ($i == 0) {
  966. //$sql .= "WHERE ".$row['name']." ".$row['operator']." '".$row['value']."' ";
  967. $sql .= "WHERE ".$row['name']." ".$row['operator']." :".$row['value']." ";
  968. }
  969. else {
  970. //$sql .= "AND ".$row['name']." ".$row['operator']." '".$row['value']."' ";
  971. $sql .= "AND ".$row['name']." ".$row['operator']." :".$row['value']." ";
  972. }
  973. //add the name and value to the params array
  974. $params[$row['name']] = $row['value'];
  975. //increment $i
  976. $i++;
  977. }
  978. }
  979. //order by
  980. if (isset($array['order_by'])) {
  981. $array['order_by'] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['order_by']);
  982. $sql .= "ORDER BY ".$array['order_by']." ";
  983. }
  984. //limit
  985. if (isset($array['limit']) && is_numeric($array['limit'])) {
  986. $sql .= "LIMIT ".$array['limit']." ";
  987. }
  988. //offset
  989. if (isset($array['offset']) && is_numeric($array['offset'])) {
  990. $sql .= "OFFSET ".$array['offset']." ";
  991. }
  992. }
  993. //execute the query, and return the results
  994. try {
  995. $prep_statement = $this->db->prepare($sql);
  996. $prep_statement->execute($params);
  997. $message["message"] = "OK";
  998. $message["code"] = "200";
  999. $message["details"][$m]["name"] = $this->name;
  1000. $message["details"][$m]["message"] = "OK";
  1001. $message["details"][$m]["code"] = "200";
  1002. if ($this->debug["sql"]) {
  1003. $message["details"][$m]["sql"] = $sql;
  1004. }
  1005. $this->message = $message;
  1006. $this->result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  1007. unset($prep_statement);
  1008. $m++;
  1009. return $this;
  1010. }
  1011. catch(PDOException $e) {
  1012. $message["message"] = "Bad Request";
  1013. $message["code"] = "400";
  1014. $message["details"][$m]["name"] = $this->name;
  1015. $message["details"][$m]["message"] = $e->getMessage();
  1016. $message["details"][$m]["code"] = "400";
  1017. if ($this->debug["sql"]) {
  1018. $message["details"][$m]["sql"] = $sql;
  1019. }
  1020. $this->message = $message;
  1021. $this->result = '';
  1022. $m++;
  1023. return $this;
  1024. }
  1025. }
  1026. private function normalize_array($array, $name) {
  1027. //get the depth of the array
  1028. $depth = $this->array_depth($array);
  1029. //before normalizing the array
  1030. //echo "before: ".$depth."<br />\n";
  1031. //echo "<pre>\n";
  1032. //print_r($array);
  1033. //echo "</pre>\n";
  1034. //normalize the array
  1035. if ($depth == 1) {
  1036. $return_array[$name][] = $array;
  1037. } else if ($depth == 2) {
  1038. $return_array[$name] = $array;
  1039. //} else if ($depth == 3) {
  1040. // $return_array[$name][] = $array;
  1041. } else {
  1042. $return_array = $array;
  1043. }
  1044. unset($array);
  1045. //after normalizing the array
  1046. $depth = $this->array_depth($new_array);
  1047. //echo "after: ".$depth."<br />\n";
  1048. //echo "<pre>\n";
  1049. //print_r($new_array);
  1050. //echo "</pre>\n";
  1051. //return the array
  1052. return $return_array;
  1053. }
  1054. public function uuid($uuid) {
  1055. $this->uuid = $uuid;
  1056. return $this;
  1057. }
  1058. public function save($array) {
  1059. //return the array
  1060. if (!is_array($array)) { echo "not an array"; return false; }
  1061. //set the message id
  1062. $m = 0;
  1063. //set the app name
  1064. if (!isset($this->app_name)) {
  1065. $this->app_name = $this->name;
  1066. }
  1067. //normalize the array structure
  1068. //$new_array = $this->normalize_array($array, $this->name);
  1069. //unset($array);
  1070. $new_array = $array;
  1071. //connect to the database if needed
  1072. if (!$this->db) {
  1073. $this->connect();
  1074. }
  1075. //debug sql
  1076. $this->debug["sql"] = true;
  1077. //start the atomic transaction
  1078. $this->db->beginTransaction();
  1079. //debug info
  1080. //echo "<pre>\n";
  1081. //print_r($new_array);
  1082. //echo "</pre>\n";
  1083. //exit;
  1084. //loop through the array
  1085. if (is_array($new_array)) foreach ($new_array as $schema_name => $schema_array) {
  1086. $this->name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $schema_name);
  1087. if (is_array($schema_array)) foreach ($schema_array as $schema_id => $array) {
  1088. //set the variables
  1089. $table_name = "v_".$this->name;
  1090. $parent_key_name = $this->singular($this->name)."_uuid";
  1091. $parent_key_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $parent_key_name);
  1092. //if the uuid is set then set parent key exists and value
  1093. //determine if the parent_key_exists
  1094. $parent_key_exists = false;
  1095. if (isset($array[$parent_key_name])) {
  1096. $parent_key_value = $array[$parent_key_name];
  1097. $parent_key_exists = true;
  1098. }
  1099. else {
  1100. if (isset($this->uuid)) {
  1101. $parent_key_exists = true;
  1102. $parent_key_value = $this->uuid;
  1103. }
  1104. else {
  1105. $parent_key_value = uuid();
  1106. }
  1107. }
  1108. //allow characters found in the uuid only.
  1109. $parent_key_value = preg_replace('#[^a-zA-Z0-9_\-]#', '', $parent_key_value);
  1110. //get the parent field names
  1111. $parent_field_names = array();
  1112. if (is_array($array)) {
  1113. foreach ($array as $key => $value) {
  1114. if (!is_array($value)) {
  1115. $parent_field_names[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $key);
  1116. }
  1117. }
  1118. }
  1119. //determine action update or delete and get the original data
  1120. if ($parent_key_exists) {
  1121. $sql = "SELECT ".implode(", ", $parent_field_names)." FROM ".$table_name." ";
  1122. $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
  1123. $prep_statement = $this->db->prepare($sql);
  1124. if ($prep_statement) {
  1125. //get the data
  1126. try {
  1127. $prep_statement->execute();
  1128. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  1129. }
  1130. catch(PDOException $e) {
  1131. echo 'Caught exception: ', $e->getMessage(), "<br/><br/>\n";
  1132. echo $sql;
  1133. exit;
  1134. }
  1135. //set the action
  1136. if (count($result) > 0) {
  1137. $action = "update";
  1138. $old_array[$schema_name] = $result;
  1139. }
  1140. else {
  1141. $action = "add";
  1142. }
  1143. }
  1144. unset($prep_statement);
  1145. unset($result);
  1146. }
  1147. else {
  1148. $action = "add";
  1149. }
  1150. //add a record
  1151. if ($action == "add") {
  1152. if (permission_exists($this->singular($this->name).'_add')) {
  1153. $params = array();
  1154. $sql = "INSERT INTO v_".$this->name." ";
  1155. $sql .= "(";
  1156. if (!$parent_key_exists) {
  1157. $sql .= $parent_key_name.", ";
  1158. }
  1159. //foreach ($parent_field_names as $field_name) {
  1160. // $sql .= check_str($field_name).", ";
  1161. //}
  1162. if (is_array($array)) {
  1163. foreach ($array as $array_key => $array_value) {
  1164. if (!is_array($array_value)) {
  1165. $array_key = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array_key);
  1166. $sql .= $array_key.", ";
  1167. }
  1168. }
  1169. }
  1170. $sql .= ") ";
  1171. $sql .= "VALUES ";
  1172. $sql .= "(";
  1173. if (!$parent_key_exists) {
  1174. $sql .= "'".$parent_key_value."', ";
  1175. }
  1176. if (is_array($array)) {
  1177. foreach ($array as $array_key => $array_value) {
  1178. if (!is_array($array_value)) {
  1179. if (strlen($array_value) == 0) {
  1180. $sql .= "null, ";
  1181. }
  1182. elseif ($array_value === "now()") {
  1183. $sql .= "now(), ";
  1184. }
  1185. else {
  1186. //$sql .= "'".check_str($array_value)."', ";
  1187. $sql .= ':'.$array_key.", ";
  1188. $params[$array_key] = trim($array_value);
  1189. }
  1190. }
  1191. }
  1192. }
  1193. $sql .= ");";
  1194. $sql = str_replace(", )", ")", $sql);
  1195. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  1196. try {
  1197. //$this->db->query(check_sql($sql));
  1198. $prep_statement = $this->db->prepare($sql);
  1199. $prep_statement->execute($params);
  1200. unset($prep_statement);
  1201. $message["message"] = "OK";
  1202. $message["code"] = "200";
  1203. $message["uuid"] = $parent_key_value;
  1204. $message["details"][$m]["name"] = $this->name;
  1205. $message["details"][$m]["message"] = "OK";
  1206. $message["details"][$m]["code"] = "200";
  1207. $message["details"][$m]["uuid"] = $parent_key_value;
  1208. if ($this->debug["sql"]) {
  1209. $message["details"][$m]["sql"] = $sql;
  1210. if (is_array($params)) {
  1211. $message["details"][$m]["params"] = $params;
  1212. }
  1213. }
  1214. unset($params);
  1215. $this->message = $message;
  1216. $m++;
  1217. }
  1218. catch(PDOException $e) {
  1219. $message["message"] = "Bad Request";
  1220. $message["code"] = "400";
  1221. $message["details"][$m]["name"] = $this->name;
  1222. $message["details"][$m]["message"] = $e->getMessage();
  1223. $message["details"][$m]["code"] = "400";
  1224. $message["details"][$m]["array"] = $array;
  1225. if ($this->debug["sql"]) {
  1226. $message["details"][$m]["sql"] = $sql;
  1227. if (is_array($params)) {
  1228. $message["details"][$m]["params"] = $params;
  1229. }
  1230. }
  1231. unset($params);
  1232. $this->message = $message;
  1233. $m++;
  1234. }
  1235. unset($sql);
  1236. }
  1237. else {
  1238. $message["name"] = $this->name;
  1239. $message["message"] = "Forbidden, does not have '".$this->singular($this->name)."_add'";
  1240. $message["code"] = "403";
  1241. $message["line"] = __line__;
  1242. $this->message[] = $message;
  1243. $m++;
  1244. }
  1245. }
  1246. //edit a specific uuid
  1247. if ($action == "update") {
  1248. if (permission_exists($this->singular($this->name).'_edit')) {
  1249. //parent data
  1250. $params = array();
  1251. $sql = "UPDATE v_".$this->name." SET ";
  1252. if (is_array($array)) {
  1253. foreach ($array as $array_key => $array_value) {
  1254. if (!is_array($array_value) && $array_key != $parent_key_name) {
  1255. $array_key = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array_key);
  1256. if (strlen($array_value) == 0) {
  1257. $sql .= $array_key." = null, ";
  1258. }
  1259. elseif ($array_value === "now()") {
  1260. $sql .= $array_key." = now(), ";
  1261. }
  1262. else {
  1263. //$sql .= $array_key." = '".check_str($array_value)."', ";
  1264. $sql .= $array_key." = :".$array_key.", ";
  1265. $params[$array_key] = trim($array_value);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
  1271. $sql = str_replace(", WHERE", " WHERE", $sql);
  1272. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  1273. try {
  1274. $prep_statement = $this->db->prepare($sql);
  1275. $prep_statement->execute($params);
  1276. //$this->db->query(check_sql($sql));
  1277. $message["message"] = "OK";
  1278. $message["code"] = "200";
  1279. $message["uuid"] = $parent_key_value;
  1280. $message["details"][$m]["name"] = $this->name;
  1281. $message["details"][$m]["message"] = "OK";
  1282. $message["details"][$m]["code"] = "200";
  1283. $message["details"][$m]["uuid"] = $parent_key_value;
  1284. if ($this->debug["sql"]) {
  1285. $message["details"][$m]["sql"] = $sql;
  1286. if (is_array($params)) {
  1287. $message["details"][$m]["params"] = $params;
  1288. }
  1289. }
  1290. unset($params);
  1291. $this->message = $message;
  1292. $m++;
  1293. unset($sql);
  1294. }
  1295. catch(PDOException $e) {
  1296. $message["message"] = "Bad Request";
  1297. $message["code"] = "400";
  1298. $message["details"][$m]["name"] = $this->name;
  1299. $message["details"][$m]["message"] = $e->getMessage();
  1300. $message["details"][$m]["code"] = "400";
  1301. if ($this->debug["sql"]) {
  1302. $message["details"][$m]["sql"] = $sql;
  1303. if (is_array($params)) {
  1304. $message["details"][$m]["params"] = $params;
  1305. }
  1306. }
  1307. unset($params);
  1308. $this->message = $message;
  1309. $m++;
  1310. }
  1311. }
  1312. else {
  1313. $message["name"] = $this->name;
  1314. $message["message"] = "Forbidden, does not have '".$this->singular($this->name)."_edit'";
  1315. $message["code"] = "403";
  1316. $message["line"] = __line__;
  1317. $this->message = $message;
  1318. $m++;
  1319. }
  1320. }
  1321. //unset the variables
  1322. unset($sql, $action);
  1323. //child data
  1324. if (is_array($array)) {
  1325. foreach ($array as $key => $value) {
  1326. if (is_array($value)) {
  1327. $table_name = "v_".$key;
  1328. $table_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $table_name);
  1329. foreach ($value as $id => $row) {
  1330. //prepare the variables
  1331. $child_name = $this->singular($key);
  1332. $child_name = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_name);
  1333. $child_key_name = $child_name."_uuid";
  1334. //determine if the parent key exists in the child array
  1335. $parent_key_exists = false;
  1336. if (!isset($array[$parent_key_name])) {
  1337. $parent_key_exists = true;
  1338. }
  1339. //determine if the uuid exists
  1340. $uuid_exists = false;
  1341. if (is_array($row)) foreach ($row as $k => $v) {
  1342. if ($child_key_name == $k) {
  1343. if (strlen($v) > 0) {
  1344. $child_key_value = trim($v);
  1345. $uuid_exists = true;
  1346. break;
  1347. }
  1348. }
  1349. else {
  1350. $uuid_exists = false;
  1351. }
  1352. }
  1353. //allow characters found in the uuid only.
  1354. $child_key_value = preg_replace('#[^a-zA-Z0-9_\-]#', '', $child_key_value);
  1355. //get the child field names
  1356. $child_field_names = array();
  1357. if (is_array($row)) {
  1358. foreach ($row as $k => $v) {
  1359. if (!is_array($v)) {
  1360. $child_field_names[] = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
  1361. }
  1362. }
  1363. }
  1364. //determine sql update or delete and get the original data
  1365. if ($uuid_exists) {
  1366. $sql = "SELECT ". implode(", ", $child_field_names)." FROM ".$table_name." ";
  1367. $sql .= "WHERE ".$child_key_name." = '".$child_key_value."' ";
  1368. $prep_statement = $this->db->prepare($sql);
  1369. if ($prep_statement) {
  1370. //get the data
  1371. $prep_statement->execute();
  1372. $child_array = $prep_statement->fetch(PDO::FETCH_ASSOC);
  1373. //set the action
  1374. if (is_array($child_array)) {
  1375. $action = "update";
  1376. }
  1377. else {
  1378. $action = "add";
  1379. }
  1380. //add to the parent array
  1381. if (is_array($child_array)) {
  1382. $old_array[$schema_name][$schema_id][$key][] = $child_array;
  1383. }
  1384. }
  1385. unset($prep_statement);
  1386. }
  1387. else {
  1388. $action = "add";
  1389. }
  1390. //update the child data
  1391. if ($action == "update") {
  1392. if (permission_exists($child_name.'_edit')) {
  1393. $sql = "UPDATE ".$table_name." SET ";
  1394. if (is_array($row)) {
  1395. foreach ($row as $k => $v) {
  1396. if (!is_array($v) && ($k != $parent_key_name || $k != $child_key_name)) {
  1397. $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
  1398. if (strlen($v) == 0) {
  1399. $sql .= $k." = null, ";
  1400. }
  1401. elseif ($v === "now()") {
  1402. $sql .= $k." = now(), ";
  1403. }
  1404. else {
  1405. //$sql .= "$k = '".check_str($v)."', ";
  1406. $sql .= $k." = :".$k.", ";
  1407. $params[$k] = trim($v);
  1408. }
  1409. }
  1410. }
  1411. }
  1412. $sql .= "WHERE ".$parent_key_name." = '".$parent_key_value."' ";
  1413. $sql .= "AND ".$child_key_name." = '".$child_key_value."' ";
  1414. $sql = str_replace(", WHERE", " WHERE", $sql);
  1415. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  1416. //$prep_statement->bindParam(':domain_uuid', $this->domain_uuid );
  1417. try {
  1418. //$this->db->query(check_sql($sql));
  1419. $prep_statement = $this->db->prepare($sql);
  1420. $prep_statement->execute($params);
  1421. unset($prep_statement);
  1422. $message["details"][$m]["name"] = $key;
  1423. $message["details"][$m]["message"] = "OK";
  1424. $message["details"][$m]["code"] = "200";
  1425. $message["details"][$m]["uuid"] = $child_key_value;
  1426. if ($this->debug["sql"]) {
  1427. $message["details"][$m]["sql"] = $sql;
  1428. if (is_array($params)) {
  1429. $message["details"][$m]["params"] = $params;
  1430. unset($params);
  1431. }
  1432. }
  1433. $this->message = $message;
  1434. $m++;
  1435. }
  1436. catch(PDOException $e) {
  1437. if ($message["code"] = "200") {
  1438. $message["message"] = "Bad Request";
  1439. $message["code"] = "400";
  1440. }
  1441. $message["details"][$m]["name"] = $key;
  1442. $message["details"][$m]["message"] = $e->getMessage();
  1443. $message["details"][$m]["code"] = "400";
  1444. if ($this->debug["sql"]) {
  1445. $message["details"][$m]["sql"] = $sql;
  1446. if (is_array($params)) {
  1447. $message["details"][$m]["params"] = $params;
  1448. unset($params);
  1449. }
  1450. }
  1451. $this->message = $message;
  1452. $m++;
  1453. }
  1454. }
  1455. else {
  1456. $message["name"] = $child_name;
  1457. $message["message"] = "Forbidden, does not have '${child_name}_edit'";
  1458. $message["code"] = "403";
  1459. $message["line"] = __line__;
  1460. $this->message = $message;
  1461. $m++;
  1462. }
  1463. } //action update
  1464. //add the child data
  1465. if ($action == "add") {
  1466. if (permission_exists($child_name.'_add')) {
  1467. //determine if child or parent key exists
  1468. $child_key_name = $child_name.'_uuid';
  1469. $parent_key_exists = false;
  1470. $child_key_exists = false;
  1471. if (is_array($row)) {
  1472. foreach ($row as $k => $v) {
  1473. if ($k == $parent_key_name) {
  1474. $parent_key_exists = true;
  1475. }
  1476. if ($k == $child_key_name) {
  1477. $child_key_exists = true;
  1478. $child_key_value = trim($v);
  1479. }
  1480. }
  1481. }
  1482. if (!$child_key_value) {
  1483. $child_key_value = uuid();
  1484. }
  1485. //build the insert
  1486. $sql = "INSERT INTO ".$table_name." ";
  1487. $sql .= "(";
  1488. if (!$parent_key_exists) {
  1489. $sql .= $this->singular($parent_key_name).", ";
  1490. }
  1491. if (!$child_key_exists) {
  1492. $sql .= $this->singular($child_key_name).", ";
  1493. }
  1494. if (is_array($row)) {
  1495. foreach ($row as $k => $v) {
  1496. if (!is_array($v)) {
  1497. $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
  1498. $sql .= $k.", ";
  1499. }
  1500. }
  1501. }
  1502. $sql .= ") ";
  1503. $sql .= "VALUES ";
  1504. $sql .= "(";
  1505. if (!$parent_key_exists) {
  1506. $sql .= "'".$parent_key_value."', ";
  1507. }
  1508. if (!$child_key_exists) {
  1509. $sql .= "'".$child_key_value."', ";
  1510. }
  1511. if (is_array($row)) {
  1512. foreach ($row as $k => $v) {
  1513. if (!is_array($v)) {
  1514. if (strlen($v) == 0) {
  1515. $sql .= "null, ";
  1516. }
  1517. elseif ($v === "now()") {
  1518. $sql .= "now(), ";
  1519. }
  1520. else {
  1521. $k = preg_replace('#[^a-zA-Z0-9_\-]#', '', $k);
  1522. //$sql .= "'".check_str($v)."', ";
  1523. $sql .= ':'.$k.", ";
  1524. $params[$k] = trim($v);
  1525. }
  1526. }
  1527. }
  1528. }
  1529. $sql .= ");";
  1530. $sql = str_replace(", )", ")", $sql);
  1531. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  1532. try {
  1533. //$this->db->query(check_sql($sql));
  1534. $prep_statement = $this->db->prepare($sql);
  1535. $prep_statement->execute($params);
  1536. unset($prep_statement);
  1537. $message["details"][$m]["name"] = $key;
  1538. $message["details"][$m]["message"] = "OK";
  1539. $message["details"][$m]["code"] = "200";
  1540. $message["details"][$m]["uuid"] = $child_key_value;
  1541. if ($this->debug["sql"]) {
  1542. $message["details"][$m]["sql"] = $sql;
  1543. if (is_array($params)) {
  1544. $message["details"][$m]["params"] = $params;
  1545. unset($params);
  1546. }
  1547. }
  1548. $this->message = $message;
  1549. $m++;
  1550. }
  1551. catch(PDOException $e) {
  1552. if ($message["code"] = "200") {
  1553. $message["message"] = "Bad Request";
  1554. $message["code"] = "400";
  1555. }
  1556. $message["details"][$m]["name"] = $key;
  1557. $message["details"][$m]["message"] = $e->getMessage();
  1558. $message["details"][$m]["code"] = "400";
  1559. if ($this->debug["sql"]) {
  1560. $message["details"][$m]["sql"] = $sql;
  1561. if (is_array($params)) {
  1562. $message["details"][$m]["params"] = $params;
  1563. unset($params);
  1564. }
  1565. }
  1566. $this->message = $message;
  1567. $m++;
  1568. }
  1569. }
  1570. else {
  1571. $message["name"] = $child_name;
  1572. $message["message"] = "Forbidden, does not have '${child_name}_add'";
  1573. $message["code"] = "403";
  1574. $message["line"] = __line__;
  1575. $this->message = $message;
  1576. $m++;
  1577. }
  1578. } //action add
  1579. //unset the variables
  1580. unset($sql, $action, $child_key_name, $child_key_value);
  1581. } // foreach value
  1582. } //is array
  1583. } //foreach array
  1584. }
  1585. } // foreach schema_array
  1586. } // foreach main array
  1587. //return the before and after data
  1588. //log this in the future
  1589. //if (is_array($old_array)) {
  1590. //normalize the array structure
  1591. //$old_array = $this->normalize_array($old_array, $this->name);
  1592. //debug info
  1593. //echo "<pre>\n";
  1594. //print_r($old_array);
  1595. //echo "</pre>\n";
  1596. //exit;
  1597. //}
  1598. //$message["new"] = $new_array;
  1599. //$message["new"]["md5"] = md5(json_encode($new_array));
  1600. $this->message = $message;
  1601. //commit the atomic transaction
  1602. $this->db->commit();
  1603. //set the action if not set
  1604. if (strlen($action) == 0) {
  1605. if (is_array($old_array)) {
  1606. $transaction_type = 'update';
  1607. }
  1608. else {
  1609. $transaction_type = 'add';
  1610. }
  1611. }
  1612. else {
  1613. $transaction_type = $action;
  1614. }
  1615. //get the UUIDs
  1616. $user_uuid = $_SESSION['user_uuid'];
  1617. //log the transaction results
  1618. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/database_transactions/app_config.php")) {
  1619. try {
  1620. $sql = "insert into v_database_transactions ";
  1621. $sql .= "(";
  1622. $sql .= "database_transaction_uuid, ";
  1623. $sql .= "domain_uuid, ";
  1624. if (isset($user_uuid) && is_uuid($user_uuid)) {
  1625. $sql .= "user_uuid, ";
  1626. }
  1627. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  1628. $sql .= "app_uuid, ";
  1629. }
  1630. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  1631. $sql .= "app_name, ";
  1632. }
  1633. $sql .= "transaction_code, ";
  1634. $sql .= "transaction_address, ";
  1635. $sql .= "transaction_type, ";
  1636. $sql .= "transaction_date, ";
  1637. $sql .= "transaction_old, ";
  1638. $sql .= "transaction_new, ";
  1639. $sql .= "transaction_result ";
  1640. $sql .= ")";
  1641. $sql .= "values ";
  1642. $sql .= "(";
  1643. $sql .= "'".uuid()."', ";
  1644. if (is_null($this->domain_uuid)) {
  1645. $sql .= "null, ";
  1646. }
  1647. else {
  1648. $sql .= "'".$this->domain_uuid."', ";
  1649. }
  1650. if (isset($user_uuid) && is_uuid($user_uuid)) {
  1651. $sql .= ":user_uuid, ";
  1652. }
  1653. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  1654. $sql .= ":app_uuid, ";
  1655. }
  1656. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  1657. $sql .= ":app_name, ";
  1658. }
  1659. $sql .= "'".$message["code"]."', ";
  1660. $sql .= ":remote_address, ";
  1661. $sql .= "'".$transaction_type."', ";
  1662. $sql .= "now(), ";
  1663. if (is_array($old_array)) {
  1664. $sql .= ":transaction_old, ";
  1665. }
  1666. else {
  1667. $sql .= "null, ";
  1668. }
  1669. if (is_array($new_array)) {
  1670. $sql .= ":transaction_new, ";
  1671. }
  1672. else {
  1673. $sql .= "null, ";
  1674. }
  1675. $sql .= ":transaction_result ";
  1676. $sql .= ")";
  1677. $statement = $this->db->prepare($sql);
  1678. if (isset($user_uuid) && is_uuid($user_uuid)) {
  1679. $statement->bindParam(':user_uuid', $user_uuid);
  1680. }
  1681. if (isset($this->app_uuid) && is_uuid($this->app_uuid)) {
  1682. $statement->bindParam(':app_uuid', $this->app_uuid);
  1683. }
  1684. if (isset($this->app_name) && strlen($this->app_name) > 0) {
  1685. $statement->bindParam(':app_name', $this->app_name);
  1686. }
  1687. $statement->bindParam(':remote_address', $_SERVER['REMOTE_ADDR']);
  1688. if (is_array($old_array)) {
  1689. $old_json = json_encode($old_array, JSON_PRETTY_PRINT);
  1690. $statement->bindParam(':transaction_old', $old_json);
  1691. }
  1692. if (is_array($new_array)) {
  1693. $new_json = json_encode($new_array, JSON_PRETTY_PRINT);
  1694. $statement->bindParam(':transaction_new', $new_json);
  1695. }
  1696. $message = json_encode($this->message, JSON_PRETTY_PRINT);
  1697. $statement->bindParam(':transaction_result', $message);
  1698. $statement->execute();
  1699. unset($sql);
  1700. }
  1701. catch(PDOException $e) {
  1702. echo $e->getMessage();
  1703. exit;
  1704. }
  1705. }
  1706. } //save method
  1707. //define singular function to convert a word in english to singular
  1708. public function singular($word) {
  1709. //"-es" is used for words that end in "-x", "-s", "-z", "-sh", "-ch" in which case you add
  1710. if (substr($word, -2) == "es") {
  1711. if (substr($word, -4) == "sses") { // eg. 'addresses' to 'address'
  1712. return substr($word,0,-2);
  1713. }
  1714. elseif (substr($word, -3) == "ses") { // eg. 'databases' to 'database' (necessary!)
  1715. return substr($word,0,-1);
  1716. }
  1717. elseif (substr($word, -3) == "ies") { // eg. 'countries' to 'country'
  1718. return substr($word,0,-3)."y";
  1719. }
  1720. elseif (substr($word, -3, 1) == "x") {
  1721. return substr($word,0,-2);
  1722. }
  1723. elseif (substr($word, -3, 1) == "s") {
  1724. return substr($word,0,-2);
  1725. }
  1726. elseif (substr($word, -3, 1) == "z") {
  1727. return substr($word,0,-2);
  1728. }
  1729. elseif (substr($word, -4, 2) == "sh") {
  1730. return substr($word,0,-2);
  1731. }
  1732. elseif (substr($word, -4, 2) == "ch") {
  1733. return substr($word,0,-2);
  1734. }
  1735. else {
  1736. return rtrim($word, "s");
  1737. }
  1738. }
  1739. else {
  1740. return rtrim($word, "s");
  1741. }
  1742. }
  1743. public function get_apps() {
  1744. //get the $apps array from the installed apps from the core and mod directories
  1745. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  1746. $x = 0;
  1747. if (is_array($config_list)) {
  1748. foreach ($config_list as &$config_path) {
  1749. include($config_path);
  1750. $x++;
  1751. }
  1752. }
  1753. $_SESSION['apps'] = $apps;
  1754. }
  1755. public function array_depth($array) {
  1756. if (is_array($array)) {
  1757. foreach ($array as $value) {
  1758. if (!isset($depth)) { $depth = 1; }
  1759. if (is_array($value)) {
  1760. $depth = $this->array_depth($value) + 1;
  1761. }
  1762. }
  1763. }
  1764. else {
  1765. $depth = 0;
  1766. }
  1767. return $depth;
  1768. }
  1769. public function domain_uuid_exists($name) {
  1770. //get the $apps array from the installed apps from the core and mod directories
  1771. if (!is_array($_SESSION['apps'])) {
  1772. $this->get_apps();
  1773. }
  1774. //search through all fields to see if domain_uuid exists
  1775. $apps = $_SESSION['apps'];
  1776. if (is_array($apps)) {
  1777. foreach ($apps as $x => &$app) {
  1778. if (is_array($app['db'])) {
  1779. foreach ($app['db'] as $y => &$row) {
  1780. if ($row['table'] == $name) {
  1781. if (is_array($row['fields'])) {
  1782. foreach ($row['fields'] as $z => $field) {
  1783. if ($field['name'] == "domain_uuid") {
  1784. return true;
  1785. }
  1786. } //foreach
  1787. } //is array
  1788. }
  1789. } //foreach
  1790. } //is array
  1791. } //foreach
  1792. } //is array
  1793. //not found
  1794. return false;
  1795. }
  1796. } //class database
  1797. } //!class_exists
  1798. //addtitional functions for sqlite
  1799. if (!function_exists('php_md5')) {
  1800. function php_md5($string) {
  1801. return md5($string);
  1802. }
  1803. }
  1804. if (!function_exists('php_unix_time_stamp')) {
  1805. function php_unix_time_stamp($string) {
  1806. return strtotime($string);
  1807. }
  1808. }
  1809. if (!function_exists('php_now')) {
  1810. function php_now() {
  1811. return date("Y-m-d H:i:s");
  1812. }
  1813. }
  1814. if (!function_exists('php_left')) {
  1815. function php_left($string, $num) {
  1816. return substr($string, 0, $num);
  1817. }
  1818. }
  1819. if (!function_exists('php_right')) {
  1820. function php_right($string, $num) {
  1821. return substr($string, (strlen($string)-$num), strlen($string));
  1822. }
  1823. }
  1824. /*
  1825. //example usage
  1826. //find
  1827. require_once "resources/classes/database.php";
  1828. $database = new database;
  1829. $database->domain_uuid = $_SESSION["domain_uuid"];
  1830. $database->type = $db_type;
  1831. $database->table = "v_extensions";
  1832. $where[0]['name'] = 'domain_uuid';
  1833. $where[0]['value'] = $_SESSION["domain_uuid"];
  1834. $where[0]['operator'] = '=';
  1835. $database->where = $where;
  1836. $order_by[0]['name'] = 'extension';
  1837. $database->order_by = $order_by;
  1838. $database->order_type = 'desc';
  1839. $database->limit = '2';
  1840. $database->offset = '0';
  1841. $database->find();
  1842. print_r($database->result);
  1843. //insert
  1844. require_once "resources/classes/database.php";
  1845. $database = new database;
  1846. $database->domain_uuid = $_SESSION["domain_uuid"];
  1847. $database->table = "v_ivr_menus";
  1848. $fields[0]['name'] = 'domain_uuid';
  1849. $fields[0]['value'] = $_SESSION["domain_uuid"];
  1850. echo $database->count();
  1851. */
  1852. ?>