-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtxplib_wrapper.php
More file actions
943 lines (800 loc) · 28.2 KB
/
Copy pathtxplib_wrapper.php
File metadata and controls
943 lines (800 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
<?php
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Textpattern Wrapper Class for Textpattern 4.0.x.
*
* Main goal for this class is to be used as a textpattern data wrapper by
* any code which needs to have access to the textpattern articles data,
* like XML-RPC, Atom, Moblogging or other external implementations.
*
* This class requires including some Textpattern files in order to work
* properly. See RPC Server implementation to view an example of the required
* files and predefined variables.
*
* @link https://web.archive.org/web/20141201035729/http://txp.kusor.com/wrapper
* @author Pedro Palazón
* @copyright 2005-2008 The Textpattern Development Team
*/
if (!defined('txpath')) {
die('txpath is undefined.');
}
include_once txpath.'/include/txp_auth.php';
if (!defined('LEAVE_TEXT_UNTOUCHED')) {
/**
* @ignore
*/
define('LEAVE_TEXT_UNTOUCHED', 0);
}
if (!defined('USE_TEXTILE')) {
/**
* @ignore
*/
define('USE_TEXTILE', 1);
}
if (!defined('CONVERT_LINEBREAKS')) {
/**
* @ignore
*/
define('CONVERT_LINEBREAKS', 2);
}
/**
* Wrapper for Textpattern.
*
* @package Wrapper
*/
class TXP_Wrapper
{
/**
* The current user.
*
* Remember to always use $this->txp_user when checking
* for permissions with this class.
*
* @var string
*/
public $txp_user = null;
/**
* Authenticated connection.
*
* @var bool
*/
public $loggedin = false;
/**
* Predefined Textpattern variables to be populated.
*
* @var array
*/
public $vars = array(
'ID',
'Title',
'Title_html',
'Body',
'Body_html',
'Excerpt',
'Excerpt_html',
'textile_excerpt',
'Image',
'textile_body',
'Keywords',
'Status',
'Posted',
'Section',
'Category1',
'Category2',
'Annotate',
'AnnotateInvite',
'AuthorID',
'Posted',
'override_form',
'url_title',
'custom_1',
'custom_2',
'custom_3',
'custom_4',
'custom_5',
'custom_6',
'custom_7',
'custom_8',
'custom_9',
'custom_10',
);
/**
* Constructor.
*
* This is used to pass user credentials
* to the wrapper.
*
* @param string $txp_user The user login name
* @param string $txpass User password
* @example
* $wrapper = new TXP_wrapper('username', 'password');
*/
public function __construct($txp_user, $txpass = null)
{
if ($this->_validate($txp_user, $txpass)) {
$this->txp_user = $txp_user;
$this->loggedin = true;
}
}
/**
* Deletes an article with the given ID.
*
* @param int $article_id The article
* @return bool TRUE on success
*/
public function deleteArticleID($article_id)
{
$article_id = assert_int($article_id);
if ($this->loggedin && has_privs('article.delete', $this->txp_user)) {
return safe_delete('textpattern', "ID = $article_id");
} elseif ($this->loggedin && has_privs('article.delete.own', $this->txp_user)) {
$r = safe_field("ID", 'textpattern', "ID = $article_id AND AuthorID = '".doSlash($this->txp_user)."'");
if ($r || has_privs('article.delete', $this->txp_user)) {
return safe_delete('textpattern', "ID = $article_id");
}
}
return false;
}
/**
* Retrieves a list of articles matching the given criteria.
*
* This method forms an SQL query from the given arguments and returns an
* array of resulting articles.
*
* This method requires authentication and at least 'article.edit.own'
* privileges. If the user doesn't have 'article.edit' privileges,
* only the user's own articles can be accessed.
*
* @param string $what The select clause
* @param string $where The where clause
* @param int $offset The offset
* @param int $limit The limit
* @param bool $slash If TRUE, escapes $where and $what
* @return array|bool Array of articles, or FALSE on failure
*/
public function getArticleList($what = '*', $where = '1', $offset = 0, $limit = 10, $slash = true)
{
if ($this->loggedin && has_privs('article.edit.own', $this->txp_user)) {
$offset = assert_int($offset);
$limit = assert_int($limit);
if ($slash) {
$where = doSlash($where);
$what = doSlash($what);
}
if (has_privs('article.edit', $this->txp_user)) {
$rs = safe_rows_start($what, 'textpattern', $where." ORDER BY Posted DESC LIMIT $offset, $limit");
} else {
$rs = safe_rows_start($what, 'textpattern', $where." AND AuthorID = '".doSlash($this->txp_user)."' ORDER BY Posted DESC LIMIT $offset, $limit");
}
$out = array();
if ($rs) {
while ($a = nextRow($rs)) {
$out[] = $a;
}
}
return $out;
}
return false;
}
/**
* Retrieves an article matching the given criteria.
*
* This method forms an SQL query from the given arguments and returns an
* article as an associative array.
*
* This method requires authentication and at least 'article.edit.own'
* privileges. If the user doesn't have 'article.edit' privileges,
* only the user's own articles can be accessed.
*
* @param string $what Select clause
* @param string $where Where clause
* @param bool $slash If TRUE, escapes $where and $what
* @return array|bool An article, or FALSE on failure
* @see TXP_Wrapper::getArticleList()
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($r = $wrapper->getArticle())
* {
* echo "Returned an article by the title '{$r['Title']}'.";
* }
*/
public function getArticle($what = '*', $where = '1', $slash = true)
{
if ($this->loggedin && has_privs('article.edit.own', $this->txp_user)) {
if ($slash) {
$what = doSlash($what);
$where = doSlash($where);
}
// Higher user groups should be able to edit any article.
if (has_privs('article.edit', $this->txp_user)) {
return safe_row($what, 'textpattern', $where);
} else {
// While restricted users should be able to edit their own
// articles only.
return safe_row($what, 'textpattern', $where." AND AuthorID = '".doSlash($this->txp_user)."'");
}
}
return false;
}
/**
* Gets an article with the given ID.
*
* This method is an shortcut for TXP_Wrapper::getArticle().
*
* @param int $article_id The article
* @param string $what The SQL select clause
* @return array|bool The article, or FALSE on failure
* @see TXP_Wrapper::getArticle()
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($r = $wrapper->getArticleID(11))
* {
* echo "Returned an article by the ID of '{$r['ID']}'.";
* }
*/
public function getArticleID($article_id, $what = '*')
{
if ($this->loggedin && has_privs('article.edit.own', $this->txp_user)) {
$article_id = assert_int($article_id);
if (has_privs('article.edit', $this->txp_user)) {
return safe_row(doSlash($what), 'textpattern', "ID = $article_id");
} else {
return safe_row(doSlash($what), 'textpattern', "ID = $article_id AND AuthorID = '".doSlash($this->txp_user)."'");
}
}
return false;
}
/**
* Updates an existing article.
*
* This method takes an array of article fields, and updates an article with
* the given ID. Supplied values are sanitised and prepared internally.
*
* @param int $article_id The article
* @param array $params The article fields to update
* @return int|bool The article id, or FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if (($id = $wrapper->updateArticleID(11, array(
* 'Title' => 'New title',
* 'Body' => 'Body text.',
* )) !== false)
* {
* echo "Updated article '{$id}'.";
* }
*/
public function updateArticleID($article_id, $params)
{
$article_id = assert_int($article_id);
$r = safe_field("ID", 'textpattern', "AuthorID = '".doSlash($this->txp_user)."' AND ID = $article_id");
if ($this->loggedin && $r && has_privs('article.edit.own', $this->txp_user)) {
// Unprivileged user, check if they can edit published articles.
$r = assert_int($r);
$oldstatus = safe_field("Status", 'textpattern', "ID = $r");
if (($oldstatus == 4 || $oldstatus == 5) && !has_privs('article.edit.published', $this->txp_user)) {
return false;
}
// If they can, let's go.
return $this->_setArticle($params, $article_id);
} elseif ($this->loggedin && has_privs('article.edit', $this->txp_user)) {
// Admin editing. Desires are behest.
return $this->_setArticle($params, $article_id);
}
return false;
}
/**
* Creates a new article.
*
* @param array $params The article fields
* @return int|bool Article ID, or FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if (($id = $wrapper->newArticle(array(
* 'Title' => 'My article',
* 'Body' => 'My body text',
* )) !== false)
* {
* echo "Created a new article with the ID of '{$id}'.";
* }
*/
public function newArticle($params)
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
if (($params['Status'] == 4 || $params['Status'] == 5) && !has_privs('article.publish', $this->txp_user)) {
$params['Status'] = 3;
}
return $this->_setArticle($params);
}
return false;
}
/**
* Gets a list of sections as an associative array.
*
* This method requires authentication and 'article' privileges.
*
* @return array|bool FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($sections = $wrapper->getSectionsList())
* {
* foreach ($sections as $section)
* {
* echo $section['title'];
* }
* }
*/
public function getSectionsList()
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
return safe_rows("*", 'txp_section', "name != 'default'");
}
return false;
}
/**
* Gets a section as an associative array.
*
* This method requires authentication and 'article' privileges.
*
* @param string $name The section name
* @return array|bool FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($section = $wrapper->getSection('my-section'))
* {
* echo $section['title'];
* }
*/
public function getSection($name)
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
$name = doSlash($name);
return safe_row("*", 'txp_section', "name = '$name'");
}
return false;
}
/**
* Gets a list of categories as an associative array.
*
* This method requires authentication and 'article' privileges.
*
* @return array|bool FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($categories = $wrapper->getCategoryList())
* {
* foreach ($categories as $category)
* {
* echo $category['title'];
* }
* }
*/
public function getCategoryList()
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
return safe_rows("*", 'txp_category', "name != 'root' AND type = 'article'");
}
return false;
}
/**
* Gets a category as an associative array.
*
* This method requires authentication and 'article' privileges.
*
* @param string $name The category name
* @return array|bool FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($category = $wrapper->getCategory('my-category'))
* {
* echo $category['title'];
* }
*/
public function getCategory($name)
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
$name = doSlash($name);
return safe_row("*", 'txp_category', "name = '$name' AND type = 'article'");
}
return false;
}
/**
* Gets a category as an associative array by ID.
*
* This method is an alternative to TXP_wrapper::getCategory().
*
* This method requires authentication and 'article' privileges.
*
* @param string $id The category ID
* @return array|bool FALSE on failure
*/
public function getCategoryID($id)
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
$id = assert_int($id);
return safe_row("*", 'txp_category', "id = $id");
}
return false;
}
/**
* Gets a category as an associative array by title.
*
* This method is an alternative to TXP_wrapper::getCategory().
*
* This method requires authentication and 'article' privileges.
*
* @param string $title The category title
* @return array|bool FALSE on failure
*/
public function getCategoryTitle($title)
{
if ($this->loggedin && has_privs('article', $this->txp_user)) {
$title = doSlash($title);
return safe_row("*", 'txp_category', "title = '$title' AND type = 'article'");
}
return false;
}
/**
* Gets an array of information about the current user.
*
* This method requires authentication. Resulting array contains all columns
* from 'txp_users' database table.
*
* @return array|bool FALSE on failure
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($user = $wrapper->getUser())
* {
* echo $user['RealName'] . ' ' . $user['email'];
* }
*/
public function getUser()
{
if ($this->loggedin) {
return safe_row("*", 'txp_users', "name = '".$this->txp_user."'");
}
return false;
}
/**
* Retrieves a page template contents with the given name.
*
* This method requires authentication and 'page' privileges.
*
* @param string $name The template
* @return string|bool The template, or FALSE on failure
*/
public function getTemplate($name)
{
if ($this->loggedin && has_privs('page', $this->txp_user)) {
$name = doSlash($name);
return safe_field("user_html", 'txp_page', "name = '$name'");
}
return false;
}
/**
* Updates a page template with the given name.
*
* This method requires authentication and 'page' privileges.
*
* @param string $name The template name
* @param string $html The template contents
* @return bool TRUE on success
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($wrapper->setTemplate('default', '<txp:site_name />'))
* {
* echo "Page template updated.";
* }
*/
public function setTemplate($name, $html)
{
if ($this->loggedin && has_privs('page', $this->txp_user)) {
$name = doSlash($name);
$html = doSlash($html);
return safe_update('txp_page', "user_html = '$html'", "name = '$name'");
}
return false;
}
/**
* Intended for updating an article's non-content fields, like categories,
* sections or keywords.
*
* This method requires authentication and 'article.edit' privileges.
*
* @param int $article_id The article
* @param string $field The field to update
* @param mixed $value The new value
* @return bool TRUE on success
* @see TXP_wrapper::updateArticleID()
* @example
* $wrapper = new TXP_wrapper('username', 'password');
* if ($wrapper->updateArticleField(11, 'Section', 'new-section'))
* {
* echo "Section updated.";
* }
*/
public function updateArticleField($article_id, $field, $value)
{
$disallow = array(
'Body',
'Body_html',
'Title',
'Title_html',
'Excerpt',
'Excerpt_html',
'textile_excerpt',
'textile_body',
'LastMod',
'LastModID',
'feed_time',
'uid',
);
if ($this->loggedin && has_privs('article.edit', $this->txp_user) && !in_array(doSlash($field), $disallow)) {
$field = doSlash($field);
$value = doSlash($value);
if ($field == 'Posted') {
$value = strtotime($value) - tz_offset();
$value = "COALESCE(FROM_UNIXTIME(0), FROM_UNIXTIME(1)) + INTERVAL $value SECOND";
$sql = "Posted = $value";
} elseif ($field == 'Status') {
$value = assert_int($value);
if (!has_privs('article.publish', $this->txp_user) && $value >= 4) {
$value = 3;
}
$sql = "Status = $value";
} else {
$sql = "$field = '$value'";
}
$sql .= ", LastMod = NOW(), LastModID = '".$this->txp_user."'";
$article_id = assert_int($article_id);
$rs = safe_update('textpattern', $sql, "ID = $article_id");
return $rs;
}
return false;
}
/**
* Creates and updates articles.
*
* @param array $incoming The article fields
* @param int $article_id The ID of the article to update
* @return int|bool The article ID on success, or FALSE on failure
* @access private
* @see TXP_wrapper::udpateArticleId()
* @see TXP_wrapper::newArticle()
*/
public function _setArticle($incoming, $article_id = null)
{
global $txpcfg;
$prefs = get_prefs();
extract($prefs);
if (!empty($incoming['Section']) && !$this->getSection($incoming['Section'])) {
return false;
}
if (!empty($incoming['Category1']) && !$this->getCategory($incoming['Category1'])) {
return false;
}
if (!empty($incoming['Category2']) && !$this->getCategory($incoming['Category2'])) {
return false;
}
if ($article_id !== null) {
$article_id = assert_int($article_id);
}
// All validation rules assumed to be passed before this point.
// Do content processing here.
$incoming_with_markup = $this->textile_main_fields($incoming, $use_textile);
$incoming['Title'] = $incoming_with_markup['Title'];
if (empty($incoming['Body_html']) && !empty($incoming['Body'])) {
if(!empty($incoming_with_markup['Body_html']))
$incoming['Body_html'] = $incoming_with_markup['Body_html'];
else // Handle the situation where no explicit html is being received - e.g. body may be html
$incoming['Body_html'] = $incoming['Body'];
}
if (empty($incoming['Excerpt_html']) && !empty($incoming['Excerpt'])) {
if(!empty($incoming_with_markup['Body_html']))
$incoming['Excerpt_html'] = $incoming_with_markup['Excerpt_html'];
else // Handle the situation where no explicit html is being received - e.g. body may be html
$incoming['Excerpt_html'] = $incoming['Excerpt'];
}
unset($incoming_with_markup);
if (empty($incoming['Posted'])) {
if ($article_id === null) {
$when = (!$article_id) ? 'NOW()' : '';
$incoming['Posted'] = $when;
} else {
// Do not override post time for existing articles unless Posted
// is present.
unset($incoming['Posted']);
}
} else {
$when = strtotime($incoming['Posted']) - tz_offset();
$when = "COALESCE(FROM_UNIXTIME(0), FROM_UNIXTIME(1)) + INTERVAL $when SECOND";
}
if ($incoming['Title'] || $incoming['Body'] || $incoming['Excerpt']) {
// Build SQL then and run query.
// Prevent data erase if not defined on the update action but it
// was on the database from a previous creation/edition time.
if ($article_id) {
$old = safe_row("*", 'textpattern', "ID = $article_id");
if (!has_privs('article.publish', $this->txp_user) && $incoming['Status'] == 4 && $old['Status'] != 4) {
$incoming['Status'] = 3;
}
foreach ($old as $key => $val) {
if (!isset($incoming[$key])) {
$incoming[$key] = $val;
}
}
} else {
if (!has_privs('article.publish', $this->txp_user) && $incoming['Status'] == 4) {
$incoming['Status'] = 3;
}
}
if (empty($incoming['Section']) && $article_id) {
$incoming['Section'] = safe_field("Section", 'textpattern', "ID = $article_id");
}
$incoming = $this->_check_keys($incoming, array(
'AuthorID' => $this->txp_user,
'Annotate' => $comments_on_default,
'AnnotateInvite' => $comments_default_invite,
'textile_body' => $use_textile,
'textile_excerpt' => $use_textile,
'url_title' => stripSpace($incoming['Title']),
));
// Build the SQL query.
$sql = array();
foreach ($incoming as $key => $val) {
if ($key == 'Posted' && $val == 'NOW()') {
$sql[] = "$key = $val";
} elseif ($key != 'ID' && $key != 'uid' && $key != 'feed_time' && $key != 'LastMod' && $key != 'LastModID') {
$sql[] = "$key = '".doSlash($val)."'";
}
}
$sql[] = "LastMod = NOW()";
$sql[] = "LastModID = '".doSlash($this->txp_user)."'";
if (!$article_id) {
$sql[] = "uid = '".doSlash(md5(uniqid(rand(), true)))."'";
}
if (!$article_id) {
if (empty($incoming['Posted'])) {
$sql[] = "feed_time = CURDATE()";
} else {
$when = strtotime($incoming['Posted']) - tz_offset();
$when = date('Y-m-d', $when);
$sql[] = "feed_time = '".doSlash($when)."'";
}
}
$sql = join(', ', $sql);
$rs = ($article_id) ? safe_update('textpattern', $sql, "ID = $article_id") : safe_insert('textpattern', $sql);
$oldstatus = ($article_id) ? $old['Status'] : '';
if (!$article_id && $rs) {
$article_id = $rs;
}
if (($incoming['Status'] >= 4 && !$article_id) || ($oldstatus != 4 && $article_id)) {
safe_update('txp_prefs', "val = NOW()", "name = 'lastmod'");
}
return $article_id;
}
return false;
}
/**
* Validates the given user credentials.
*
* @param string $user The username
* @param string $password The password
* @return bool TRUE on success
* @access private
*/
public function _validate($user, $password = null)
{
if ($password !== null) {
$r = txp_validate($user, $password);
} else {
$r = true;
}
if ($r) {
// Update the last access time.
$safe_user = doSlash($user);
safe_update('txp_users', "last_access = NOW()", "name = '$safe_user'");
return true;
}
return false;
}
/**
* Validates and filters the given article fields.
*
* Checks if the given parameters are appropriate for the article.
*
* @param array $incoming The incoming associative array
* @param array $default An associative array containing default values for the desired keys
* @return array Filtered data array
* @access private
*/
public function _check_keys($incoming, $default = array())
{
$out = array();
// Strip off unsuited keys.
foreach ($incoming as $key => $val) {
if (in_array($key, $this->vars)) {
$out[$key] = $val;
}
}
foreach ($this->vars as $def_key) {
// Add those ones nonexistent in the incoming array.
if (!array_key_exists($def_key, $out)) {
$out[$def_key] = '';
}
// Setup the provided default value, if any, only when the incoming
// value is empty.
if (array_key_exists($def_key, $default) && empty($out[$def_key])) {
$out[$def_key] = $default[$def_key];
}
}
return $out;
}
/**
* Apply Textile to the main article fields.
*
* This is duplicated from txp_article.php.
*
* @param array $incoming The incoming fields
* @param bool $use_textile Use Textile or not
* @return array The $incoming array formatted
* @access private
*/
public function textile_main_fields($incoming, $use_textile = 1)
{
global $txpcfg;
$textile = new \Textpattern\Textile\Parser();
if (!empty($event) and $event == 'article') {
$incoming['Title_plain'] = $incoming['Title'];
}
if ($incoming['textile_body'] == USE_TEXTILE) {
$incoming['Title'] = $textile->textileEncode($incoming['Title']);
}
$incoming['url_title'] = preg_replace('|[\x00-\x1f#%+/?\x7f]|', '', $incoming['url_title']);
$incoming['Body_html'] = TXP_Wrapper::format_field($incoming['Body'], $incoming['textile_body'], $textile);
$incoming['Excerpt_html'] = TXP_Wrapper::format_field($incoming['Excerpt'], $incoming['textile_excerpt'], $textile);
return $incoming;
}
/**
* Formats a article field according to the given options.
*
* @param string $field The field contents
* @param int $format Either LEAVE_TEXT_UNTOUCHED, CONVERT_LINEBREAKS, USE_TEXTILE
* @param Textile An instance of Textile
* @return string HTML formatted field
* @access private
*/
public function format_field($field, $format, $textile)
{
switch ($format) {
case LEAVE_TEXT_UNTOUCHED:
$html = trim($field);
break;
case CONVERT_LINEBREAKS:
$html = nl2br(trim($field));
break;
case USE_TEXTILE:
$html = $textile->parse($field);
break;
}
return $html;
}
}