@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -include_once(MODX_BASE_PATH.'assets/lib/APIHelpers.class.php'); |
|
| 3 | -include_once(MODX_BASE_PATH.'assets/snippets/DocLister/lib/jsonHelper.class.php'); |
|
| 4 | -include_once(MODX_BASE_PATH.'assets/snippets/DocLister/lib/DLCollection.class.php'); |
|
| 2 | +include_once(MODX_BASE_PATH . 'assets/lib/APIHelpers.class.php'); |
|
| 3 | +include_once(MODX_BASE_PATH . 'assets/snippets/DocLister/lib/jsonHelper.class.php'); |
|
| 4 | +include_once(MODX_BASE_PATH . 'assets/snippets/DocLister/lib/DLCollection.class.php'); |
|
| 5 | 5 | |
| 6 | 6 | abstract class MODxAPI extends MODxAPIhelpers |
| 7 | 7 | {
|
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | public function __construct(DocumentParser $modx, $debug = false) |
| 27 | 27 | {
|
| 28 | 28 | $this->modx = $modx; |
| 29 | - if(function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()){
|
|
| 29 | + if (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) {
|
|
| 30 | 30 | throw new Exception('Magic Quotes is a deprecated and mostly useless setting that should be disabled. Please ask your server administrator to disable it in php.ini or in your webserver config.');
|
| 31 | 31 | } |
| 32 | 32 | |
@@ -34,40 +34,40 @@ discard block |
||
| 34 | 34 | $this->_decodedFields = new DLCollection($this->modx); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function setDebug($flag){
|
|
| 37 | + public function setDebug($flag) {
|
|
| 38 | 38 | $this->_debug = (bool)$flag; |
| 39 | 39 | return $this; |
| 40 | 40 | } |
| 41 | - public function getDebug(){
|
|
| 41 | + public function getDebug() {
|
|
| 42 | 42 | return $this->_debug; |
| 43 | 43 | } |
| 44 | - public function getDefaultFields(){
|
|
| 44 | + public function getDefaultFields() {
|
|
| 45 | 45 | return $this->default_field; |
| 46 | 46 | } |
| 47 | 47 | final public function modxConfig($name, $default = null) |
| 48 | 48 | {
|
| 49 | 49 | return APIHelpers::getkey($this->modx->config, $name, $default); |
| 50 | 50 | } |
| 51 | - public function addQuery($q){
|
|
| 52 | - if(is_scalar($q) && !empty($q)){
|
|
| 51 | + public function addQuery($q) {
|
|
| 52 | + if (is_scalar($q) && !empty($q)) {
|
|
| 53 | 53 | $this->_query[] = $q; |
| 54 | 54 | } |
| 55 | 55 | return $this; |
| 56 | 56 | } |
| 57 | - public function getQueryList(){
|
|
| 57 | + public function getQueryList() {
|
|
| 58 | 58 | return $this->_query; |
| 59 | 59 | } |
| 60 | 60 | final public function query($SQL) |
| 61 | 61 | {
|
| 62 | - if($this->getDebug()){
|
|
| 62 | + if ($this->getDebug()) {
|
|
| 63 | 63 | $this->addQuery($SQL); |
| 64 | 64 | } |
| 65 | 65 | return empty($SQL) ? null : $this->modx->db->query($SQL); |
| 66 | 66 | } |
| 67 | - final public function escape($value){
|
|
| 68 | - if(!is_scalar($value)){
|
|
| 67 | + final public function escape($value) {
|
|
| 68 | + if (!is_scalar($value)) {
|
|
| 69 | 69 | $value = ''; |
| 70 | - }else{
|
|
| 70 | + } else {
|
|
| 71 | 71 | $value = $this->modx->db->escape($value); |
| 72 | 72 | } |
| 73 | 73 | return $value; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | final public function clearCache($fire_events = null, $custom = false) |
| 112 | 112 | {
|
| 113 | 113 | $IDs = array(); |
| 114 | - if($custom === false) {
|
|
| 114 | + if ($custom === false) {
|
|
| 115 | 115 | $this->modx->clearCache(); |
| 116 | 116 | include_once(MODX_MANAGER_PATH . 'processors/cache_sync.class.processor.php'); |
| 117 | 117 | $sync = new synccache(); |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | $sync->setCachepath($path); |
| 120 | 120 | $sync->setReport(false); |
| 121 | 121 | $sync->emptyCache(); |
| 122 | - }else {
|
|
| 123 | - if(is_scalar($custom)){
|
|
| 122 | + } else {
|
|
| 123 | + if (is_scalar($custom)) {
|
|
| 124 | 124 | $custom = array($custom); |
| 125 | 125 | } |
| 126 | 126 | switch ($this->modx->config['cache_type']) {
|
@@ -130,11 +130,11 @@ discard block |
||
| 130 | 130 | default: |
| 131 | 131 | $cacheFile = ".pageCache.php"; |
| 132 | 132 | } |
| 133 | - if(is_array($custom)) {
|
|
| 134 | - foreach($custom as $id) {
|
|
| 135 | - $tmp = glob(MODX_BASE_PATH."assets/cache/docid_" . $id . $cacheFile); |
|
| 136 | - foreach($tmp as $file){
|
|
| 137 | - if(is_readable($file)){
|
|
| 133 | + if (is_array($custom)) {
|
|
| 134 | + foreach ($custom as $id) {
|
|
| 135 | + $tmp = glob(MODX_BASE_PATH . "assets/cache/docid_" . $id . $cacheFile); |
|
| 136 | + foreach ($tmp as $file) {
|
|
| 137 | + if (is_readable($file)) {
|
|
| 138 | 138 | unlink($file); |
| 139 | 139 | } |
| 140 | 140 | $IDs[] = $id; |
@@ -145,8 +145,8 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | $this->invokeEvent('OnSiteRefresh', array('IDs' => $IDs), $fire_events);
|
| 147 | 147 | } |
| 148 | - public function switchObject($id){
|
|
| 149 | - switch(true){
|
|
| 148 | + public function switchObject($id) {
|
|
| 149 | + switch (true) {
|
|
| 150 | 150 | //Если загружен другой объект - не тот, с которым мы хотим временно поработать |
| 151 | 151 | case ($this->getID() != $id && $id): |
| 152 | 152 | $obj = clone $this; |
@@ -162,11 +162,11 @@ discard block |
||
| 162 | 162 | } |
| 163 | 163 | return $obj; |
| 164 | 164 | } |
| 165 | - public function useIgnore($flag = true){
|
|
| 165 | + public function useIgnore($flag = true) {
|
|
| 166 | 166 | $this->ignoreError = $flag ? 'IGNORE' : ''; |
| 167 | 167 | return $this; |
| 168 | 168 | } |
| 169 | - public function hasIgnore(){
|
|
| 169 | + public function hasIgnore() {
|
|
| 170 | 170 | return (bool)$this->ignoreError; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | } |
| 314 | 314 | return $out; |
| 315 | 315 | } |
| 316 | - final public function fieldPKName(){
|
|
| 316 | + final public function fieldPKName() {
|
|
| 317 | 317 | return $this->pkName; |
| 318 | 318 | } |
| 319 | 319 | final public function makeTable($table) |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | } |
| 330 | 330 | $out = array(); |
| 331 | 331 | foreach ($data as $item) {
|
| 332 | - if($item !== ''){
|
|
| 332 | + if ($item !== '') {
|
|
| 333 | 333 | $out[] = $this->escape($item); |
| 334 | 334 | } |
| 335 | 335 | } |
@@ -344,18 +344,18 @@ discard block |
||
| 344 | 344 | foreach ($field as $_field) {
|
| 345 | 345 | $val = $this->get($_field); |
| 346 | 346 | if ($val != '') |
| 347 | - $where[] = "`".$this->escape($_field)."` = '".$this->escape($val)."'"; |
|
| 347 | + $where[] = "`" . $this->escape($_field) . "` = '" . $this->escape($val) . "'"; |
|
| 348 | 348 | } |
| 349 | - $where = implode(' AND ',$where);
|
|
| 349 | + $where = implode(' AND ', $where);
|
|
| 350 | 350 | } else {
|
| 351 | 351 | $where = ''; |
| 352 | 352 | $val = $this->get($field); |
| 353 | 353 | if ($val != '') |
| 354 | - $where = "`".$this->escape($field)."` = '".$this->escape($val)."'"; |
|
| 354 | + $where = "`" . $this->escape($field) . "` = '" . $this->escape($val) . "'"; |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | if ($where != '') {
|
| 358 | - $sql = $this->query("SELECT `" . $this->escape($PK) . "` FROM " . $this->makeTable($table) . " WHERE ".$where);
|
|
| 358 | + $sql = $this->query("SELECT `" . $this->escape($PK) . "` FROM " . $this->makeTable($table) . " WHERE " . $where);
|
|
| 359 | 359 | $id = $this->modx->db->getValue($sql); |
| 360 | 360 | if (is_null($id) || (!$this->newDoc && $id == $this->getID())) {
|
| 361 | 361 | $flag = true; |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | * @param string $field имя поля |
| 440 | 440 | * @return boolean |
| 441 | 441 | */ |
| 442 | - public function isJsonField($field){
|
|
| 442 | + public function isJsonField($field) {
|
|
| 443 | 443 | return (is_scalar($field) && in_array($field, $this->jsonFields)); |
| 444 | 444 | } |
| 445 | 445 | |
@@ -448,8 +448,8 @@ discard block |
||
| 448 | 448 | * @param string $field имя поля |
| 449 | 449 | * @return $this |
| 450 | 450 | */ |
| 451 | - public function markAsDecode($field){
|
|
| 452 | - if(is_scalar($field)){
|
|
| 451 | + public function markAsDecode($field) {
|
|
| 452 | + if (is_scalar($field)) {
|
|
| 453 | 453 | $this->_decodedFields->set($field, false); |
| 454 | 454 | } |
| 455 | 455 | return $this; |
@@ -460,8 +460,8 @@ discard block |
||
| 460 | 460 | * @param string $field имя поля |
| 461 | 461 | * @return $this |
| 462 | 462 | */ |
| 463 | - public function markAsEncode($field){
|
|
| 464 | - if(is_scalar($field)){
|
|
| 463 | + public function markAsEncode($field) {
|
|
| 464 | + if (is_scalar($field)) {
|
|
| 465 | 465 | $this->_decodedFields->set($field, true); |
| 466 | 466 | } |
| 467 | 467 | return $this; |
@@ -471,9 +471,9 @@ discard block |
||
| 471 | 471 | * Пометить все поля как запакованные |
| 472 | 472 | * @return $this |
| 473 | 473 | */ |
| 474 | - public function markAllEncode(){
|
|
| 474 | + public function markAllEncode() {
|
|
| 475 | 475 | $this->_decodedFields->clear(); |
| 476 | - foreach($this->jsonFields as $field){
|
|
| 476 | + foreach ($this->jsonFields as $field) {
|
|
| 477 | 477 | $this->markAsEncode($field); |
| 478 | 478 | } |
| 479 | 479 | return $this; |
@@ -483,9 +483,9 @@ discard block |
||
| 483 | 483 | * Пометить все поля как распакованные |
| 484 | 484 | * @return $this |
| 485 | 485 | */ |
| 486 | - public function markAllDecode(){
|
|
| 486 | + public function markAllDecode() {
|
|
| 487 | 487 | $this->_decodedFields->clear(); |
| 488 | - foreach($this->jsonFields as $field){
|
|
| 488 | + foreach ($this->jsonFields as $field) {
|
|
| 489 | 489 | $this->markAsDecode($field); |
| 490 | 490 | } |
| 491 | 491 | return $this; |
@@ -495,8 +495,8 @@ discard block |
||
| 495 | 495 | * Получить список не запакованных полей |
| 496 | 496 | * @return array |
| 497 | 497 | */ |
| 498 | - public function getNoEncodeFields(){
|
|
| 499 | - return $this->_decodedFields->filter(function($value){
|
|
| 498 | + public function getNoEncodeFields() {
|
|
| 499 | + return $this->_decodedFields->filter(function($value) {
|
|
| 500 | 500 | return ($value === false); |
| 501 | 501 | }); |
| 502 | 502 | } |
@@ -505,8 +505,8 @@ discard block |
||
| 505 | 505 | * Получить список не распакованных полей |
| 506 | 506 | * @return array |
| 507 | 507 | */ |
| 508 | - public function getNoDecodeFields(){
|
|
| 509 | - return $this->_decodedFields->filter(function($value){
|
|
| 508 | + public function getNoDecodeFields() {
|
|
| 509 | + return $this->_decodedFields->filter(function($value) {
|
|
| 510 | 510 | return ($value === true); |
| 511 | 511 | }); |
| 512 | 512 | } |
@@ -516,12 +516,12 @@ discard block |
||
| 516 | 516 | * @param string $field имя поля |
| 517 | 517 | * @return boolean |
| 518 | 518 | */ |
| 519 | - public function isDecodableField($field){
|
|
| 519 | + public function isDecodableField($field) {
|
|
| 520 | 520 | $data = $this->get($field); |
| 521 | 521 | /** |
| 522 | 522 | * Если поле скалярного типа и оно не распаковывалось раньше |
| 523 | 523 | */ |
| 524 | - return (is_scalar($data) && is_scalar($field) && $this->_decodedFields->get($field)===true); |
|
| 524 | + return (is_scalar($data) && is_scalar($field) && $this->_decodedFields->get($field) === true); |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | /** |
@@ -529,11 +529,11 @@ discard block |
||
| 529 | 529 | * @param string $field имя поля |
| 530 | 530 | * @return boolean |
| 531 | 531 | */ |
| 532 | - public function isEncodableField($field){
|
|
| 532 | + public function isEncodableField($field) {
|
|
| 533 | 533 | /** |
| 534 | 534 | * Если поле было распаковано ранее и еще не упаковано |
| 535 | 535 | */ |
| 536 | - return (is_scalar($field) && $this->_decodedFields->get($field)===false); |
|
| 536 | + return (is_scalar($field) && $this->_decodedFields->get($field) === false); |
|
| 537 | 537 | } |
| 538 | 538 | |
| 539 | 539 | /** |
@@ -542,13 +542,13 @@ discard block |
||
| 542 | 542 | * @param bool $store обновить распакованное поле |
| 543 | 543 | * @return array ассоциативный массив с данными из json строки |
| 544 | 544 | */ |
| 545 | - public function decodeField($field, $store = false){
|
|
| 545 | + public function decodeField($field, $store = false) {
|
|
| 546 | 546 | $out = array(); |
| 547 | - if($this->isDecodableField($field)){
|
|
| 547 | + if ($this->isDecodableField($field)) {
|
|
| 548 | 548 | $data = $this->get($field); |
| 549 | 549 | $out = jsonHelper::jsonDecode($data, array('assoc' => true), true);
|
| 550 | 550 | } |
| 551 | - if($store){
|
|
| 551 | + if ($store) {
|
|
| 552 | 552 | $this->field[$field] = $out; |
| 553 | 553 | $this->markAsDecode($field); |
| 554 | 554 | } |
@@ -559,8 +559,8 @@ discard block |
||
| 559 | 559 | * Декодирование всех json полей |
| 560 | 560 | * @return $this |
| 561 | 561 | */ |
| 562 | - protected function decodeFields(){
|
|
| 563 | - foreach($this->getNoDecodeFields() as $field => $flag){
|
|
| 562 | + protected function decodeFields() {
|
|
| 563 | + foreach ($this->getNoDecodeFields() as $field => $flag) {
|
|
| 564 | 564 | $this->decodeField($field, true); |
| 565 | 565 | } |
| 566 | 566 | return $this; |
@@ -572,13 +572,13 @@ discard block |
||
| 572 | 572 | * @param bool $store обновить запакованное поле |
| 573 | 573 | * @return array json строка |
| 574 | 574 | */ |
| 575 | - public function encodeField($field, $store = false){
|
|
| 575 | + public function encodeField($field, $store = false) {
|
|
| 576 | 576 | $out = null; |
| 577 | - if($this->isEncodableField($field)){
|
|
| 577 | + if ($this->isEncodableField($field)) {
|
|
| 578 | 578 | $data = $this->get($field); |
| 579 | 579 | $out = json_encode($data); |
| 580 | 580 | } |
| 581 | - if($store){
|
|
| 581 | + if ($store) {
|
|
| 582 | 582 | $this->field[$field] = $out; |
| 583 | 583 | $this->markAsEncode($field); |
| 584 | 584 | } |
@@ -589,8 +589,8 @@ discard block |
||
| 589 | 589 | * Запаковка всех json полей |
| 590 | 590 | * @return $this |
| 591 | 591 | */ |
| 592 | - protected function encodeFields(){
|
|
| 593 | - foreach($this->getNoEncodeFields() as $field => $flag){
|
|
| 592 | + protected function encodeFields() {
|
|
| 593 | + foreach ($this->getNoEncodeFields() as $field => $flag) {
|
|
| 594 | 594 | $this->encodeField($field, true); |
| 595 | 595 | } |
| 596 | 596 | return $this; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | final public function escape($value){
|
| 68 | 68 | if(!is_scalar($value)){
|
| 69 | 69 | $value = ''; |
| 70 | - }else{
|
|
| 70 | + } else{
|
|
| 71 | 71 | $value = $this->modx->db->escape($value); |
| 72 | 72 | } |
| 73 | 73 | return $value; |
@@ -95,7 +95,9 @@ discard block |
||
| 95 | 95 | final public function list_log($flush = false) |
| 96 | 96 | {
|
| 97 | 97 | echo '<pre>' . print_r(APIHelpers::sanitarTag($this->log), true) . '</pre>'; |
| 98 | - if ($flush) $this->clearLog(); |
|
| 98 | + if ($flush) {
|
|
| 99 | + $this->clearLog(); |
|
| 100 | + } |
|
| 99 | 101 | return $this; |
| 100 | 102 | } |
| 101 | 103 | |
@@ -119,7 +121,7 @@ discard block |
||
| 119 | 121 | $sync->setCachepath($path); |
| 120 | 122 | $sync->setReport(false); |
| 121 | 123 | $sync->emptyCache(); |
| 122 | - }else {
|
|
| 124 | + } else {
|
|
| 123 | 125 | if(is_scalar($custom)){
|
| 124 | 126 | $custom = array($custom); |
| 125 | 127 | } |
@@ -206,7 +208,9 @@ discard block |
||
| 206 | 208 | } else {
|
| 207 | 209 | if ($this->issetField($key) && is_scalar($this->field[$key])) {
|
| 208 | 210 | $tmp = "`{$key}`='{$this->escape($this->field[$key])}'";
|
| 209 | - } else throw new Exception("{$key} is invalid <pre>" . print_r($this->field[$key], true) . "</pre>");
|
|
| 211 | + } else {
|
|
| 212 | + throw new Exception("{$key} is invalid <pre>" . print_r($this->field[$key], true) . "</pre>");
|
|
| 213 | + } |
|
| 210 | 214 | } |
| 211 | 215 | if (!empty($tmp)) {
|
| 212 | 216 | if ($id == '') {
|
@@ -248,18 +252,24 @@ discard block |
||
| 248 | 252 | {
|
| 249 | 253 | if (is_scalar($data) && !empty($data)) {
|
| 250 | 254 | $json = json_decode($data); |
| 251 | - } else throw new Exception("json is not string with json data");
|
|
| 255 | + } else {
|
|
| 256 | + throw new Exception("json is not string with json data");
|
|
| 257 | + } |
|
| 252 | 258 | |
| 253 | 259 | if ($this->jsonError($json)) {
|
| 254 | 260 | if (isset($callback) && is_callable($callback)) {
|
| 255 | 261 | call_user_func_array($callback, array($json)); |
| 256 | 262 | } else {
|
| 257 | - if (isset($callback)) throw new Exception("Can't call callback JSON unpack <pre>" . print_r($callback, 1) . "</pre>");
|
|
| 263 | + if (isset($callback)) {
|
|
| 264 | + throw new Exception("Can't call callback JSON unpack <pre>" . print_r($callback, 1) . "</pre>");
|
|
| 265 | + } |
|
| 258 | 266 | foreach ($json as $key => $val) {
|
| 259 | 267 | $this->set($key, $val); |
| 260 | 268 | } |
| 261 | 269 | } |
| 262 | - } else throw new Exception('Error from JSON decode: <pre>' . print_r($data, 1) . '</pre>');
|
|
| 270 | + } else {
|
|
| 271 | + throw new Exception('Error from JSON decode: <pre>' . print_r($data, 1) . '</pre>');
|
|
| 272 | + } |
|
| 263 | 273 | |
| 264 | 274 | return $this; |
| 265 | 275 | } |
@@ -270,7 +280,9 @@ discard block |
||
| 270 | 280 | if (isset($callback) && is_callable($callback)) {
|
| 271 | 281 | $data = call_user_func_array($callback, array($data)); |
| 272 | 282 | } else {
|
| 273 | - if (isset($callback)) throw new Exception("Can't call callback JSON pre pack <pre>" . print_r($callback, 1) . "</pre>");
|
|
| 283 | + if (isset($callback)) {
|
|
| 284 | + throw new Exception("Can't call callback JSON pre pack <pre>" . print_r($callback, 1) . "</pre>");
|
|
| 285 | + } |
|
| 274 | 286 | } |
| 275 | 287 | $json = json_encode($data); |
| 276 | 288 | |
@@ -343,15 +355,17 @@ discard block |
||
| 343 | 355 | $where = array(); |
| 344 | 356 | foreach ($field as $_field) {
|
| 345 | 357 | $val = $this->get($_field); |
| 346 | - if ($val != '') |
|
| 347 | - $where[] = "`".$this->escape($_field)."` = '".$this->escape($val)."'"; |
|
| 358 | + if ($val != '') {
|
|
| 359 | + $where[] = "`".$this->escape($_field)."` = '".$this->escape($val)."'"; |
|
| 360 | + } |
|
| 348 | 361 | } |
| 349 | 362 | $where = implode(' AND ',$where);
|
| 350 | 363 | } else {
|
| 351 | 364 | $where = ''; |
| 352 | 365 | $val = $this->get($field); |
| 353 | - if ($val != '') |
|
| 354 | - $where = "`".$this->escape($field)."` = '".$this->escape($val)."'"; |
|
| 366 | + if ($val != '') {
|
|
| 367 | + $where = "`".$this->escape($field)."` = '".$this->escape($val)."'"; |
|
| 368 | + } |
|
| 355 | 369 | } |
| 356 | 370 | |
| 357 | 371 | if ($where != '') {
|