PageRenderTime 74ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/mongodb.php

https://github.com/MilkZoft/zan
PHP | 590 lines | 590 code | 0 blank | 0 comment | 1 complexity | 8793f65d5c3ba602b9ce3601591d561c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_MongoDB extends ZP_Load
  6. {
  7. private $collection = null;
  8. private $condition = false;
  9. private static $connection = false;
  10. private $data = false;
  11. private $fields;
  12. private $hint = false;
  13. public $json = null;
  14. private $limit = false;
  15. private $query;
  16. private $row;
  17. private $skip = false;
  18. private $sort = array("_id" => 1);
  19. private $values;
  20. public function __construct()
  21. {
  22. $this->helper("debugging");
  23. $this->config("database");
  24. $this->connect();
  25. }
  26. public function collection($collection = null)
  27. {
  28. if (!is_null($collection)) {
  29. $this->collection = $collection;
  30. }
  31. }
  32. public function command($command)
  33. {
  34. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->command($command);
  35. }
  36. public function connect()
  37. {
  38. if (!self::$connection) {
  39. try {
  40. $this->Mongo = new MongoClient();
  41. if (!$this->Mongo) {
  42. throw new Exception(e("Connection Error"), 1);
  43. }
  44. $this->Mongo->selectDb(DB_NOSQL_DATABASE);
  45. } catch(Exception $e) {
  46. getException($e);
  47. }
  48. }
  49. }
  50. public function countAll($collection)
  51. {
  52. $this->collection = isset($collection) ? $collection : $this->collection;
  53. if ($this->collection) {
  54. return $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $collection)->count();
  55. } else {
  56. return false;
  57. }
  58. }
  59. public function countByQuery($query)
  60. {
  61. $this->find($query, false);
  62. return (is_object($this->Cursor)) ? $this->Cursor->count() : false;
  63. }
  64. private function data()
  65. {
  66. if ($this->Cursor->count() === 0) {
  67. return false;
  68. } else {
  69. $this->Cursor->sort($this->sort);
  70. if ($this->limit > 0) {
  71. $this->Cursor->limit($this->limit);
  72. $this->limit = false;
  73. }
  74. if ($this->skip > 0) {
  75. $this->Cursor->skip($this->skip);
  76. }
  77. if (is_array($this->hint)) {
  78. $this->Cursor->hint($this->hint);
  79. }
  80. $data = iterator_to_array($this->Cursor, false);
  81. }
  82. return $data;
  83. }
  84. public function delete($criteria, $collection, $justOne = true, $safe = true)
  85. {
  86. $this->collection = isset($collection) ? $collection : $this->collection;
  87. if (is_null($this->collection) or !$criteria) {
  88. return false;
  89. }
  90. if ($justOne and $safe) {
  91. $options = array("justOne" => true, "w" => true);
  92. } elseif ($justOne) {
  93. $options = array("justOne" => true);
  94. } elseif ($safe) {
  95. $options = array("w" => true);
  96. }
  97. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->remove($criteria, $options);
  98. return true;
  99. }
  100. public function deleteBy($field = false, $value = false, $justOne = true, $safe = true)
  101. {
  102. return $this->delete(array($field => $value), $justOne, $safe);
  103. }
  104. public function deleteFile($_id)
  105. {
  106. $GridFS = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->getGridFS();
  107. $ID = new MongoId($_id);
  108. $GridFS->delete($ID);
  109. return true;
  110. }
  111. public function drop($collection = null)
  112. {
  113. if ($collection) {
  114. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $collection)->drop();
  115. } else {
  116. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->drop();
  117. }
  118. return true;
  119. }
  120. public function ensureIndex($index = false, $order = "ASC", $unique = false)
  121. {
  122. if ($index and $order === "ASC" and $unique) {
  123. $this->Mongo->ensureIndex(array($index => 1), array("unique" => true));
  124. } elseif ($index and $order === "DESC" and $unique) {
  125. $this->Mongo->ensureIndex(array($index => -1), array("unique" => true));
  126. } elseif ($index and $order === "ASC") {
  127. $this->Mongo->ensureIndex(array($index => 1));
  128. } elseif ($index and $order === "DESC") {
  129. $this->Mongo->ensureIndex(array($index => -1));
  130. }
  131. return false;
  132. }
  133. public function find($query = null, $collection = null)
  134. {
  135. $this->collection = isset($collection) ? $collection : $this->collection;
  136. if (is_null($query)) {
  137. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find();
  138. } else {
  139. if (!is_array($query) and is_string($query)) {
  140. $query = json_decode($query, true);
  141. }
  142. if ($this->condition) {
  143. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find($query, $this->condition);
  144. unset($this->condition);
  145. $this->condition = false;
  146. }
  147. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find($query);
  148. }
  149. return $this->data();
  150. }
  151. public function findAll($collection = null, $group = null, $order = null, $limit = null)
  152. {
  153. if ($collection) {
  154. $this->collection = $collection;
  155. }
  156. if (is_null($this->collection)) {
  157. return false;
  158. }
  159. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find();
  160. return $this->data();
  161. }
  162. public function findBy($field, $value, $collection = null)
  163. {
  164. if ($collection) {
  165. $this->collection = $collection;
  166. }
  167. if (is_null($this->collection)) {
  168. return false;
  169. }
  170. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find(array($field => $value));
  171. return $this->data();
  172. }
  173. public function findByID($ID, $collection = null)
  174. {
  175. if ($collection) {
  176. $this->collection = $collection;
  177. }
  178. if (is_null($this->collection)) {
  179. return false;
  180. }
  181. if (strlen($ID) === 24) {
  182. $ID = new MongoId($ID);
  183. }
  184. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find(array("_id" => $ID));
  185. return $this->data();
  186. }
  187. public function findFirst($collection = null)
  188. {
  189. if ($collection) {
  190. $this->collection = $collection;
  191. }
  192. if (is_null($this->collection)) {
  193. return false;
  194. }
  195. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->findOne();
  196. return $this->data();
  197. }
  198. public function findLast($collection = null)
  199. {
  200. if ($collection) {
  201. $this->collection = $collection;
  202. }
  203. if (is_null($this->collection)) {
  204. return false;
  205. }
  206. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find();
  207. $this->sort("_id", "DESC");
  208. $this->limit(1);
  209. return $this->data();
  210. }
  211. public function get($type = "AND", $array, $return = false, $add = true)
  212. {
  213. if ($add and !$return) {
  214. if ($type === "AND") {
  215. foreach ($array as $field => $value) {
  216. $this->json .= '"'. $field .'" : "'. $value .'", ';
  217. }
  218. } elseif ($type === "OR") {
  219. $keys = array_keys($array);
  220. $j = 0;
  221. $or = false;
  222. foreach ($array as $values) {
  223. if (is_array($values)) {
  224. for ($i = 0; $i <= count($values) - 1; $i++) {
  225. if (!$or) {
  226. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  227. if ($last === "]") {
  228. $this->json .= ', "$or":[{';
  229. } else {
  230. $this->json .= '"$or":[{';
  231. }
  232. $or = true;
  233. }
  234. if ($j === count($keys) - 1) {
  235. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'"';
  236. } else {
  237. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'", ';
  238. }
  239. }
  240. }
  241. $j++;
  242. }
  243. $this->json .= '}]';
  244. }
  245. } else {
  246. if ($type === "AND") {
  247. $i = 0;
  248. foreach ($array as $field => $value) {
  249. if ($i === count($array) - 1) {
  250. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  251. if ($last === "]") {
  252. $this->json .= ', "'. $field .'" : "'. $value .'"';
  253. } else {
  254. $this->json .= '"'. $field .'" : "'. $value .'"';
  255. }
  256. } else {
  257. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  258. if ($last === "]") {
  259. $this->json .= ', "'. $field .'" : "'. $value .'", ';
  260. } else {
  261. $this->json .= '"'. $field .'" : "'. $value .'", ';
  262. }
  263. }
  264. }
  265. } elseif ($type === "OR") {
  266. $keys = array_keys($array);
  267. $j = 0;
  268. $or = false;
  269. foreach ($array as $values) {
  270. if (is_array($values)) {
  271. for ($i = 0; $i <= count($values) - 1; $i++) {
  272. if (!$or) {
  273. $last = substr($this->json, strlen($this->json) - 1, strlen($this->json));
  274. if ($last === "]") {
  275. $this->json .= ', "$or":[{';
  276. } else {
  277. $this->json .= '"$or":[{';
  278. }
  279. $or = true;
  280. }
  281. if ($j === count($keys) - 1) {
  282. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'"';
  283. } else {
  284. $this->json .= '"'. $keys[$j] .'" : "'. $values[$i] .'", ';
  285. }
  286. }
  287. }
  288. $j++;
  289. }
  290. $this->json .= '}]';
  291. }
  292. }
  293. if ($return) {
  294. $return = $this->json;
  295. empty($this->json);
  296. return "{". $return ."}";
  297. }
  298. }
  299. public function getAllFiles($collection = null)
  300. {
  301. if ($collection) {
  302. $this->collection = $collection;
  303. }
  304. if (is_null($this->collection)) {
  305. return false;
  306. }
  307. $GridFS = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->getGridFS();
  308. $Cursor = $GridFS->find();
  309. $i = 0;
  310. foreach ($Cursor as $Object) {
  311. $files[$i]["filename"] = $Object->getFilename();
  312. $files[$i]["content"] = $Object->getBytes();
  313. $i++;
  314. }
  315. return $files;
  316. }
  317. public function getFile($_id, $collection = null, $mimeType = "image/jpeg", $return = false)
  318. {
  319. if ($collection) {
  320. $this->collection = $collection;
  321. }
  322. if (is_null($this->collection)) {
  323. return false;
  324. }
  325. $GridFS = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->getGridFS();
  326. $ID = new MongoId($_id);
  327. $file = $GridFS->findOne(array("_id" => $ID));
  328. if ($return) {
  329. return $file;
  330. } else {
  331. header("Content-Type: $mimeType");
  332. print $file->getBytes();
  333. exit();
  334. }
  335. }
  336. public function getLastID($collection = null)
  337. {
  338. if ($collection) {
  339. $this->collection = $collection;
  340. }
  341. if (is_null($this->collection)) {
  342. return false;
  343. }
  344. $this->Cursor = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->find();
  345. $this->sort("_id", "DESC");
  346. $this->limit(1);
  347. $data = $this->data();
  348. return $data["_id"];
  349. }
  350. public function getNext()
  351. {
  352. return (is_object($this->Cursor)) ? $this->Cursor->getNext() : false;
  353. }
  354. public function hint($field, $order = "ASC")
  355. {
  356. if ($order === "ASC") {
  357. $this->hint[$field] = 1;
  358. } else {
  359. $this->hint[$field] = -1;
  360. }
  361. }
  362. public function insert($collection = null, $data = null, $_id = true)
  363. {
  364. if ($collection and is_array($data)) {
  365. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $collection)->insert($data, array("_id" => $_id));
  366. return true;
  367. } elseif (is_array($this->data)) {
  368. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->insert($this->data, array("_id" => $_id));
  369. unset($this->data);
  370. $this->data = array();
  371. return true;
  372. }
  373. return false;
  374. }
  375. public function limit($limit = 1)
  376. {
  377. $this->limit = ($limit > 0) ? $limit : false;
  378. }
  379. public function operator($field = null, $operator = "<", $value = 0, $json = false)
  380. {
  381. if ($operator === "<") {
  382. return (!$json) ? array($field => array('$lt' => $value)) : '';
  383. } elseif ($operator === ">") {
  384. return (!$json) ? array($field => array('$gt' => $value)) : '';
  385. } elseif ($operator === "<=") {
  386. return (!$json) ? array($field => array('$lte' => $value)) : '';
  387. } elseif ($operator === ">=") {
  388. return (!$json) ? array($field => array('$gte' => $value)) : '';
  389. } elseif ($operator === "!=" or $operator === "<>") {
  390. return (!$json) ? array($field => array('$ne' => $value)) : '';
  391. } elseif ($operator === "in") {
  392. return (!$json) ? array($field => array('$in' => $value)) : '';
  393. } elseif ($operator === "all") {
  394. return (!$json) ? array($field => array('$all' => $value)) : '';
  395. } elseif ($operator === "exists") {
  396. return (!$json) ? array($field => array('$exists' => $value)) : '';
  397. } elseif ($operator === "inc" or $operator === "++") {
  398. return (!$json) ? array($field => array('$inc' => $value)) : '';
  399. } elseif ($operator === "or" or $operator === "||") {
  400. if (is_array($field)) {
  401. return (!$json) ? array('$or' => $field) : '';
  402. } else {
  403. return (!$json) ? array('$or' => array($field => $value)) : '';
  404. }
  405. } elseif ($operator === "set") {
  406. return (!$json) ? array('$set' => array($field => $value)) : '';
  407. } elseif ($operator === "unset") {
  408. return (!$json) ? array('$unset' => array($field => $value)) : '';
  409. } elseif ($operator === "push") {
  410. return (!$json) ? array('$push' => array($field => $value)) : '';
  411. } elseif ($operator === "pushAll") {
  412. return (!$json) ? array('$pushAll' => $field) : '';
  413. } elseif ($operator === "addToSet") {
  414. if (is_array($field)) {
  415. return (!$json) ? array('$addToSet' => $field) : '';
  416. } else {
  417. return (!$json) ? array('$addToSet' => array($field => $value)) : '';
  418. }
  419. } elseif ($operator === "pop") {
  420. return (!$json) ? array('$pop' => array($field => $value)) : '';
  421. }
  422. return false;
  423. }
  424. public function regex($regex, $field)
  425. {
  426. $Regex = new MongoRegex($regex);
  427. return $this->find(array($field => $regex));
  428. }
  429. public function rows()
  430. {
  431. return $this->Cursor->count();
  432. }
  433. public function save($option = null, $_id = true)
  434. {
  435. if (is_null($option)) {
  436. $this->insert($_id);
  437. } elseif (is_array($option)) {
  438. $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->save($option);
  439. }
  440. }
  441. public function set($field, $value)
  442. {
  443. $this->data[$field] = $value;
  444. }
  445. public function skip($skip = 1)
  446. {
  447. $this->skip = ($skip > 0) ? $skip : false;
  448. }
  449. public function slice($field, $count = 1)
  450. {
  451. $this->condition = (object) array($field => array('$slice' => $count));
  452. }
  453. public function sort($field, $order = "ASC")
  454. {
  455. $this->sort[$field] = ($order === "ASC") ? 1 : -1;
  456. }
  457. public function update($criteria = false, $update = false, $options = false)
  458. {
  459. if (is_null($this->collection) or !$criteria) {
  460. return false;
  461. }
  462. $options = ($options) ? $options : array("upsert" => true);
  463. if (!$update and is_array($this->data)) {
  464. $update = $this->data;
  465. }
  466. $response = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->update($criteria, array('$set' => $update));
  467. return isset($response["updatedExisting"]) ? $response["updatedExisting"] : false;
  468. }
  469. public function upload($fname = "file")
  470. {
  471. $GridFS = $this->Mongo->selectCollection(DB_NOSQL_DATABASE, $this->collection)->getGridFS();
  472. $name = FILES($fname, "name");
  473. return $GridFS->storeUpload($fname, $name);
  474. }
  475. }