-
Notifications
You must be signed in to change notification settings - Fork 19
/
story.class.php
2265 lines (1912 loc) · 79.2 KB
/
story.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 2.1 |
// +---------------------------------------------------------------------------+
// | story.class.php |
// | |
// | Geeklog Story Abstraction. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2006-2013 by the following authors: |
// | |
// | Authors: Michael Jervis, mike AT fuckingbrit DOT com |
// +---------------------------------------------------------------------------+
// | |
// | This program 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; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program 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 this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
/**
* This file provides a class to represent a story, or article. It provides a
* finite state machine for articles. Switching them between the various states:
* 1) Post Data
* 2) Display Mode
* 3) Edit Mode
* 4) Database Mode
*
* @package Geeklog
* @filesource
* @version 0.1
* @since GL 1.4.2
* @copyright Copyright © 2006-2009
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
* @author Michael Jervis, mike AT fuckingbrit DOT com
*/
/**
* Constants for stories:
* Loading from database:
*/
define('STORY_LOADED_OK', 1);
define('STORY_INVALID_SID', -1);
define('STORY_PERMISSION_DENIED', -2);
define('STORY_EDIT_DENIED', -3);
/**
* Constants for Stories:
* Saving to database
*/
define('STORY_SAVED', 2);
define('STORY_SAVED_SUBMISSION', 3);
/**
* Constants for Stories:
* Loading from request.
*/
define('STORY_DUPLICATE_SID', -4);
define('STORY_EXISTING_NO_EDIT_PERMISSION', -5);
define('STORY_NO_ACCESS_PARAMS', -6);
define('STORY_EMPTY_REQUIRED_FIELDS', -7);
define('STORY_NO_ACCESS_TOPIC', -8);
/**
* Constants for our magic loader
*/
define('STORY_AL_ALPHANUM', 0);
define('STORY_AL_NUMERIC', 1);
define('STORY_AL_CHECKBOX', 2);
define('STORY_AL_ANYTHING', 3);
class Story
{
//*************************************************************************/
// Variables:
// Public
/**
* Mode, either 'admin' for in the admin screens, or submission, for
* when the user is using submit.php. Controls tons of stuff.
*/
var $mode = 'admin';
/**
* Type of story. User submission or normal editor entered stuff.
* Will be 'submission' if it's from the submission queue.
*/
var $type = 'article';
//Private
/**
* PRIVATE MEMBER VARIABLES: Things that make up a story.
*/
var $_sid;
var $_title;
var $_page_title;
var $_meta_description;
var $_meta_keywords;
var $_introtext;
var $_bodytext;
var $_text_version;
var $_postmode;
var $_uid;
var $_draft_flag;
var $_date;
var $_hits;
var $_numemails;
var $_comment_expire;
var $_comments;
var $_trackbacks;
var $_related;
var $_featured;
var $_show_topic_icon;
var $_commentcode;
var $_trackbackcode;
var $_statuscode;
var $_expire;
var $_advanced_editor_mode;
var $_frontpage;
var $_cache_time;
var $_owner_id;
var $_group_id;
var $_perm_owner;
var $_perm_group;
var $_perm_members;
var $_perm_anon;
/**
* Misc display fields we also load from the database for a story:
*/
var $_username;
var $_fullname;
var $_photo;
var $_email;
var $_tid;
var $_topic;
var $_imageurl;
/**
* The original SID of the article, cached incase it's changed:
*/
var $_originalSid;
/**
* The access level.
*/
var $_access;
/**
* Array of images uploaded for the story.
*/
var $_storyImages;
/**
* Magic array used for cheating when loading/saving stories from/to db.
* List of database field names (which are translated into member variables
* by prepending _ to the value) as pointers to whether or not they are used
* to save data. Everything with a save value of 1 will be saved, those with
* a save value of 0 will just be loaded.
* This allows us to automate the loading of story, user and topic from a
* database result array, and generate saving of a story, from the same
* magic array.
*/
var $_dbFields = array(
'sid' => 1,
'uid' => 1,
'draft_flag' => 1,
'date' => 1,
'title' => 1,
'page_title' => 1,
'meta_description' => 1,
'meta_keywords' => 1,
'introtext' => 1,
'bodytext' => 1,
'text_version' => 1,
'hits' => 1,
'numemails' => 1,
'comments' => 1,
'trackbacks' => 1,
'related' => 1,
'featured' => 1,
'show_topic_icon' => 1,
'commentcode' => 1,
'comment_expire' => 1,
'trackbackcode' => 1,
'statuscode' => 1,
'expire' => 1,
'postmode' => 1,
'advanced_editor_mode' => 1,
'frontpage' => 1,
'cache_time' => 1,
'owner_id' => 1,
'group_id' => 1,
'perm_owner' => 1,
'perm_group' => 1,
'perm_members' => 1,
'perm_anon' => 1,
'imageurl' => 0,
'tid' => 0,
'topic' => 0,
'access' => 0,
'photo' => 0,
'email' => 0,
);
/**
* Magic array used for loading basic data from posted form. Of form:
* postfield -> numeric, target, used with COM_applyFilter. Some fields
* have exceptions applied
*/
var $_postFields = array(
'uid' => array(
STORY_AL_NUMERIC,
'_uid',
),
//'tid' => array
// (
// STORY_AL_ALPHANUM,
// '_tid'
// ),
'page_title' => array(
STORY_AL_ANYTHING,
'_page_title',
),
'meta_description' => array(
STORY_AL_ANYTHING,
'_meta_description',
),
'meta_keywords' => array(
STORY_AL_ANYTHING,
'_meta_keywords',
),
'show_topic_icon' => array(
STORY_AL_CHECKBOX,
'_show_topic_icon',
),
'draft_flag' => array(
STORY_AL_CHECKBOX,
'_draft_flag',
),
'statuscode' => array(
STORY_AL_NUMERIC,
'_statuscode',
),
'featured' => array(
STORY_AL_NUMERIC,
'_featured',
),
'frontpage' => array(
STORY_AL_NUMERIC,
'_frontpage',
),
'commentcode' => array(
STORY_AL_NUMERIC,
'_commentcode',
),
'trackbackcode' => array(
STORY_AL_NUMERIC,
'_trackbackcode',
),
'postmode' => array(
STORY_AL_ALPHANUM,
'_postmode',
),
'story_hits' => array(
STORY_AL_NUMERIC,
'_hits',
),
'story_comments' => array(
STORY_AL_NUMERIC,
'_comments',
),
'story_emails' => array(
STORY_AL_NUMERIC,
'_numemails',
),
'story_trackbacks' => array(
STORY_AL_NUMERIC,
'_trackbacks',
),
'cache_time' => array(
STORY_AL_NUMERIC,
'_cache_time',
),
'owner_id' => array(
STORY_AL_NUMERIC,
'_owner_id',
),
'group_id' => array(
STORY_AL_NUMERIC,
'_group_id',
),
'type' => array(
STORY_AL_ALPHANUM,
'type',
),
'hits' => array(
STORY_AL_NUMERIC,
'_hits',
),
'comments' => array(
STORY_AL_NUMERIC,
'_comments',
),
'trackbacks' => array(
STORY_AL_NUMERIC,
'_trackbacks',
),
);
//End Private
// End Variables.
/**************************************************************************/
/**
* Constructor, creates a story, taking a (geeklog) database object.
*
* @param $mode string Story class mode, either 'admin' or 'submission'
*/
public function __construct($mode = 'admin')
{
\Geeklog\Autoload::load('gltext');
$this->mode = $mode;
}
/**
* Check to see if there is any content in the story, for
* bothering to preview testing really.
*
* @return bool trim(title+intro+body) != ''
*/
public function hasContent()
{
if (trim($this->_title) != '') {
return true;
}
if (trim($this->_introtext) != '') {
return true;
}
if (trim($this->_bodytext) != '') {
return true;
}
return false;
}
/**
* Loads a story object from an array (that's come back from the db..)
* Used from loadFromDatabase, and used on it's own from story list
* pages.
*
* @param $story array Story array from db
*/
public function loadFromArray($story)
{
/* Use the magic cheat array to quickly reload the whole story
* from the database result array, doing the quick stripslashes.
*/
reset($this->_dbFields);
while (list($fieldName, $save) = each($this->_dbFields)) {
$varName = '_' . $fieldName;
if (array_key_exists($fieldName, $story)) {
// This is meaningless, and have a negative effect. (bug #0001655)
// $this->{$varname} = stripslashes($story[$fieldname]);
$this->{$varName} = $story[$fieldName];
}
}
if (array_key_exists('username', $story)) {
$this->_username = $story['username'];
}
if (array_key_exists('fullname', $story)) {
$this->_fullname = $story['fullname'];
}
// Overwrite the date with the timestamp.
if (!empty($story['unixdate'])) {
$this->_date = $story['unixdate'];
} else {
$this->_date = time();
}
if (!empty($story['expireunix'])) {
$this->_expire = $story['expireunix'];
} else {
$this->_expire = 0;
}
if (!empty($story['cmt_expire_unix'])) {
$this->_comment_expire = $story['cmt_expire_unix'];
} else {
$this->_comment_expire = 0;
}
// Store the original SID
$this->_originalSid = $this->_sid;
}
/**
* Load a Story object from the sid specified, returning a status result.
* The result will either be a permission denied message, invalid SID
* message, or a loaded ok message. If it's loaded ok, then we've got all
* the exciting gubbins here.
* Only used from story admin and submit.php!
*
* @param string $sid Story Identifier, valid geeklog story id from the db.
* @param string $mode 'edit'|'view'|'clone'|'editsubmission'
* @return int from a constant.
*/
public function loadFromDatabase($sid, $mode = 'edit')
{
global $_TABLES, $_CONF, $_USER, $topic;
$sid = DB_escapeString(COM_applyFilter($sid));
$sql = array();
if (!empty($sid) && (($mode === 'edit') || ($mode === 'view') || ($mode === 'clone'))) {
if (empty($topic)) {
$topic_sql = ' AND ta.tdefault = 1';
} else {
$topic_sql = " AND ta.tid = '{$topic}'";
}
/* Original
$sql['mysql'] = "SELECT STRAIGHT_JOIN s.*, UNIX_TIMESTAMP(s.date) AS unixdate, UNIX_TIMESTAMP(s.expire) AS expireunix, UNIX_TIMESTAMP(s.comment_expire) AS cmt_expire_unix, "
. "u.username, u.fullname, u.photo, u.email, t.topic, t.imageurl " . "FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t " . "WHERE (s.uid = u.uid) AND (s.tid = t.tid) AND (sid = '$sid')";
*/
$sql['mysql'] = "SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, UNIX_TIMESTAMP(s.expire) AS expireunix, UNIX_TIMESTAMP(s.comment_expire) AS cmt_expire_unix, u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl
FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t, {$_TABLES['topic_assignments']} AS ta
WHERE ta.type = 'article' AND ta.id = sid {$topic_sql} AND (s.uid = u.uid) AND (ta.tid = t.tid) AND (sid = '$sid')";
$sql['pgsql'] = "SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, UNIX_TIMESTAMP(s.expire) as expireunix, UNIX_TIMESTAMP(s.comment_expire) as cmt_expire_unix, u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl
FROM {$_TABLES['stories']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t, {$_TABLES['topic_assignments']} AS ta
WHERE ta.type = 'article' AND ta.id = sid AND ta.tdefault = 1 AND (s.uid = u.uid) AND (ta.tid = t.tid) AND (sid = '$sid')";
} elseif (!empty($sid) && ($mode === 'editsubmission')) {
/* Original
$sql['mysql'] = 'SELECT STRAIGHT_JOIN s.*, UNIX_TIMESTAMP(s.date) AS unixdate, '
. 'u.username, u.fullname, u.photo, u.email, t.topic, t.imageurl, t.group_id, ' . 't.perm_owner, t.perm_group, t.perm_members, t.perm_anon ' . 'FROM ' . $_TABLES['storysubmission'] . ' AS s, ' . $_TABLES['users'] . ' AS u, ' . $_TABLES['topics'] . ' AS t WHERE (s.uid = u.uid) AND' . ' (s.tid = t.tid) AND (sid = \'' . $sid . '\')';
$sql['pgsql'] = 'SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, '
. 'u.username, u.fullname, u.photo, u.email, t.topic, t.imageurl, t.group_id, ' . 't.perm_owner, t.perm_group, t.perm_members, t.perm_anon ' . 'FROM ' . $_TABLES['storysubmission'] . ' AS s, ' . $_TABLES['users'] . ' AS u, ' . $_TABLES['topics'] . ' AS t WHERE (s.uid = u.uid) AND' . ' (s.tid = t.tid) AND (sid = \'' . $sid . '\')';
*/
$sql['mysql'] = "SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl, t.group_id, t.perm_owner, t.perm_group, t.perm_members, t.perm_anon
FROM {$_TABLES['storysubmission']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t, {$_TABLES['topic_assignments']} AS ta
WHERE (s.uid = u.uid) AND (ta.tid = t.tid) AND (sid = '$sid')
AND ta.type = 'article' AND ta.id = sid AND ta.tdefault = 1";
$sql['pgsql'] = "SELECT s.*, UNIX_TIMESTAMP(s.date) AS unixdate, u.username, u.fullname, u.photo, u.email, t.tid, t.topic, t.imageurl, t.group_id, t.perm_owner, t.perm_group, t.perm_members, t.perm_anon
FROM {$_TABLES['storysubmission']} AS s, {$_TABLES['users']} AS u, {$_TABLES['topics']} AS t, {$_TABLES['topic_assignments']} AS ta
WHERE (s.uid = u.uid) AND (ta.tid = t.tid) AND (sid = '$sid')
AND ta.type = 'article' AND ta.id = sid AND ta.tdefault = 1";
} elseif ($mode === 'edit') {
$this->_sid = COM_makesid();
$this->_old_sid = $this->_sid;
if (isset($_CONF['draft_flag'])) {
$this->_draft_flag = $_CONF['draft_flag'];
} else {
$this->_draft_flag = 0;
}
if (isset($_CONF['show_topic_icon'])) {
$this->_show_topic_icon = $_CONF['show_topic_icon'];
} else {
$this->_show_topic_icon = 1;
}
if (isset($_CONF['default_cache_time_article'])) {
$this->_cache_time = $_CONF['default_cache_time_article'];
} else {
$this->_cache_time = 0;
}
if (COM_isAnonUser()) {
$this->_uid = 1;
} else {
$this->_uid = $_USER['uid'];
}
$this->_date = $this->_expire = time();
if ($_CONF['article_comment_close_enabled']) {
$this->_comment_expire = time() +
($_CONF['article_comment_close_days'] * 86400);
} else {
$this->_comment_expire = 0;
}
$this->_commentcode = $_CONF['comment_code'];
$this->_trackbackcode = $_CONF['trackback_code'];
$this->_title = '';
$this->_page_title = '';
$this->_meta_description = '';
$this->_meta_keywords = '';
$this->_introtext = '';
$this->_bodytext = '';
if (isset($_CONF['frontpage'])) {
$this->_frontpage = $_CONF['frontpage'];
} else {
$this->_frontpage = 1;
}
$this->_text_version = GLTEXT_LATEST_VERSION;
$this->_hits = 0;
$this->_comments = 0;
$this->_trackbacks = 0;
$this->_numemails = 0;
if (($_CONF['advanced_editor'] && $_USER['advanced_editor']) &&
($_CONF['postmode'] !== 'plaintext') &&
($_CONF['postmode'] !== 'wikitext')
) {
$this->_advanced_editor_mode = 1;
$this->_postmode = 'adveditor';
} else {
$this->_postmode = $_CONF['postmode'];
$this->_advanced_editor_mode = 0;
}
$this->_statuscode = 0;
$this->_featured = 0;
$this->_cache_time = $_CONF['default_cache_time_article'];
if (COM_isAnonUser()) {
$this->_owner_id = 1;
} else {
$this->_owner_id = $_USER['uid'];
}
if (isset($_GROUPS['Story Admin'])) {
$this->_group_id = $_GROUPS['Story Admin'];
} else {
$this->_group_id = SEC_getFeatureGroup('story.edit');
}
$array = array();
SEC_setDefaultPermissions($array, $_CONF['default_permissions_story']);
$this->_perm_owner = $array['perm_owner'];
$this->_perm_group = $array['perm_group'];
$this->_perm_anon = $array['perm_anon'];
$this->_perm_members = $array['perm_members'];
} else {
$this->loadFromArgsArray($_POST);
}
// if we have SQL, load from it
if (!empty($sql)) {
$result = DB_query($sql);
if ($result) {
$story = DB_fetchArray($result, false);
if ($story == null) {
return STORY_INVALID_SID;
}
$this->loadFromArray($story);
/**
* The above SQL also got the story owner's username etc. from
* the DB. If the user doing the cloning is different from the
* original author, we need to fix those here.
*/
if (($mode === 'clone') && ($this->_uid != $_USER['uid'])) {
$this->_uid = $_USER['uid'];
$story['owner_id'] = $this->_uid;
$uResult = DB_query("SELECT username, fullname, photo, email FROM {$_TABLES['users']} WHERE uid = {$_USER['uid']}");
list($this->_username, $this->_fullname, $this->_photo, $this->_email) = DB_fetchArray($uResult);
}
if (!isset($story['owner_id'])) {
$story['owner_id'] = 1;
}
$access = SEC_hasAccess(
$story['owner_id'], $story['group_id'],
$story['perm_owner'], $story['perm_group'],
$story['perm_members'], $story['perm_anon']
);
//$this->_access = min($access, SEC_hasTopicAccess($this->_tid));
//$this->_access = min($access, TOPIC_hasMultiTopicAccess('article', $sid));
if ($mode !== 'view') {
// When editing an article they need access to all topics article is assigned to plus edit access to article itself
$this->_access = min($access, TOPIC_hasMultiTopicAccess('article', $sid));
} else {
// When viewing a article we only care about if it has access to the current topic and article
$this->_access = min($access, TOPIC_hasMultiTopicAccess('article', $sid, $topic));
}
if ($this->_access == 0) {
return STORY_PERMISSION_DENIED;
} elseif ($this->_access == 2 && $mode !== 'view') {
return STORY_EDIT_DENIED;
} elseif ((($this->_access == 2) && ($mode == 'view')) && (($this->_draft_flag == 1) || ($this->_date > time()))) {
return STORY_INVALID_SID;
}
} else {
return STORY_INVALID_SID;
}
}
if ($mode === 'editsubmission') {
if (isset($_CONF['draft_flag'])) {
$this->_draft_flag = $_CONF['draft_flag'];
} else {
$this->_draft_flag = 1;
}
if (isset($_CONF['show_topic_icon'])) {
$this->_show_topic_icon = $_CONF['show_topic_icon'];
} else {
$this->_show_topic_icon = 1;
}
$this->_commentcode = $_CONF['comment_code'];
$this->_trackbackcode = $_CONF['trackback_code'];
$this->_featured = 0;
$this->_cache_time = $_CONF['default_cache_time_article'];
$this->_expire = time();
if ($_CONF['article_comment_close_enabled']) {
$this->_comment_expire = time() +
($_CONF['article_comment_close_days'] * 86400);
} else {
$this->_comment_expire = 0;
}
if (isset($_CONF['frontpage'])) {
$this->_frontpage = $_CONF['frontpage'];
} else {
$this->_frontpage = 1;
}
$this->_text_version = GLTEXT_LATEST_VERSION;
$this->_comments = 0;
$this->_trackbacks = 0;
$this->_numemails = 0;
$this->_statuscode = 0;
$this->_owner_id = $this->_uid;
} elseif ($mode === 'clone') {
// new story, new sid ...
$this->_sid = COM_makeSid();
$this->_old_sid = $this->_sid;
// assign ownership to current user
if (COM_isAnonUser()) {
$this->_uid = 1;
} else {
$this->_uid = $_USER['uid'];
}
$this->_owner_id = $this->_uid;
// use current date + time
$this->_date = $this->_expire = time();
// if the original story uses comment expire, update the time
if ($this->_comment_expire != 0) {
$this->_comment_expire = time() + ($_CONF['article_comment_close_days'] * 86400);
}
// reset counters
$this->_hits = 0;
$this->_comments = 0;
$this->_trackbacks = 0;
$this->_numemails = 0;
}
$this->sanitizeData();
return STORY_LOADED_OK;
}
/**
* Saves the story in it's final state to the database.
* Handles all the SID magic etc.
*
* @return int status result from a constant list.
*/
public function saveToDatabase()
{
global $_TABLES, $_DB_dbms;
$tids = TOPIC_getTopicIdsForObject('topic');
$archive_tid = DB_getItem($_TABLES['topics'], 'tid', 'archive_flag=1');
if (!empty($tids) && !empty($archive_tid)) {
if (in_array($archive_tid, $tids)) {
$this->_featured = 0;
$this->_frontpage = 0;
$this->_statuscode = STORY_ARCHIVE_ON_EXPIRE;
}
}
/* if a featured, non-draft, that goes live straight away, unfeature
* other stories in same topic:
*/
if ($this->_featured == '1') {
// there can only be one non-draft featured story
if ($this->_draft_flag == 0 AND $this->_date <= time()) {
if ($this->_frontpage == 1) {
// un-feature any featured frontpage story
DB_query("UPDATE {$_TABLES['stories']} SET featured = 0 WHERE featured = 1 AND draft_flag = 0 AND frontpage = 1 AND date <= NOW()");
}
// un-feature any featured story in the same topic
//DB_query("UPDATE {$_TABLES['stories']} SET featured = 0 WHERE featured = 1 AND draft_flag = 0 AND tid = '{$this->_tid}' AND date <= NOW()");
$tids = TOPIC_getTopicIdsForObject('topic');
if (!empty($tids)) {
DB_query("UPDATE {$_TABLES['stories']} s, {$_TABLES['topic_assignments']} ta SET s.featured = 0 WHERE s.featured = 1 AND s.draft_flag = 0 AND (ta.tid IN ('" . implode("','", $tids) . "')) AND ta.type = 'article' AND ta.id = s.sid AND s.date <= NOW()");
}
}
}
$oldArticleExists = false;
$currentSidExists = false;
// Fix up old sid => new sid stuff
$checkSid = DB_escapeString($this->_originalSid); // needed below
if ($this->_sid != $this->_originalSid) {
/* The sid has changed. Load from request will have
* ensured that if the new sid exists an error has
* been thrown, but we need to know if the old sid
* actually existed (as opposed to being a generated
* sid that was then thrown away) to reduce the sheer
* number of SQL queries we do.
*/
$newSid = DB_escapeString($this->_sid);
$sql = "SELECT 1 FROM {$_TABLES['stories']} WHERE sid='{$checkSid}'";
$result = DB_query($sql);
if ($result && (DB_numRows($result) > 0)) {
$oldArticleExists = true;
}
if ($oldArticleExists) {
// Move Comments
$sql = "UPDATE {$_TABLES['comments']} SET sid='$newSid' WHERE type='article' AND sid='$checkSid'";
DB_query($sql);
// Move Images
$sql = "UPDATE {$_TABLES['article_images']} SET ai_sid = '{$newSid}' WHERE ai_sid = '{$checkSid}'";
DB_query($sql);
// Move trackbacks
$sql = "UPDATE {$_TABLES['trackback']} SET sid='{$newSid}' WHERE sid='{$checkSid}' AND type='article'";
DB_query($sql);
}
}
// Acquire Comment Count
$sql = "SELECT COUNT(1) FROM {$_TABLES['comments']} WHERE type='article' AND sid='{$this->_sid}'";
$result = DB_query($sql);
if ($result && (DB_numRows($result) == 1)) {
$array = DB_fetchArray($result);
$this->_comments = $array[0];
} else {
$this->_comments = 0;
}
/* Format dates for storage: */
/*
* Doing this here would use the webserver's timezone, but we need
* to use the DB server's timezone so that ye olde timezone hack
* still works. See use of FROM_UNIXTIME in the SQL below.
*
* $this->_date = date('Y-m-d H:i:s', $this->_date);
* $this->_expire = date('Y-m-d H:i:s', $this->_expire);
*
*/
// Get the related URLs
$this->_related = implode("\n", STORY_extractLinks($this->DisplayElements('introtext') . ' ' . $this->DisplayElements('bodytext')));
$fields = '';
$values = '';
reset($this->_dbFields);
$this->_text_version = GLTEXT_LATEST_VERSION;
// Apply HTML filter to the text just before save
// with the permissions of current editor
$this->_introtext = GLText::applyHTMLFilter(
$this->_introtext,
$this->_postmode,
'story.edit',
$this->_text_version
);
$this->_bodytext = GLText::applyHTMLFilter(
$this->_bodytext,
$this->_postmode,
'story.edit',
$this->_text_version
);
/* This uses the database field array to generate a SQL Statement. This
* means that when adding new fields to save and load, all we need to do
* is add the field name to the array, and the code will magically cope.
*/
while (list($fieldName, $save) = each($this->_dbFields)) {
if ($save === 1) {
$varName = '_' . $fieldName;
$fields .= $fieldName . ', ';
if (($fieldName === 'date') || ($fieldName === 'expire') || ($fieldName === 'comment_expire')) {
// let the DB server do this conversion (cf. timezone hack)
$values .= 'FROM_UNIXTIME(' . $this->{$varName} . '), ';
} else {
if ($this->{$varName} === '') {
$values .= "'', ";
} else {
if (is_numeric($this->{$varName})) {
$values .= DB_escapeString($this->{$varName}) . ', ';
} else {
$values .= '\'' . DB_escapeString($this->{$varName}) . '\', ';
}
}
}
}
}
$fields = substr($fields, 0, strlen($fields) - 2);
$values = substr($values, 0, strlen($values) - 2);
DB_save($_TABLES['stories'], $fields, $values);
// Save Topics selected
TOPIC_saveTopicSelectionControl('article', $this->_sid);
if ($oldArticleExists) {
// Clean up the old story
DB_delete($_TABLES['stories'], 'sid', $checkSid);
// Delete Topic Assignments for this old article id since we just created new ones
TOPIC_deleteTopicAssignments('article', $checkSid);
}
if ($this->type === 'submission') {
// there might be a submission, clean it up
DB_delete($_TABLES['storysubmission'], 'sid', $checkSid);
}
return STORY_SAVED;
}
/**
* Loads a story from the post data. This is the most exciting function in
* the whole entire world. First it'll clean up that horrible Magic Quotes
* crap. Then it'll do all Geeklog's funky security stuff, anti XSS, anti
* SQL Injection. Yay.
*
* @param array $array
* @return int
*/
public function loadFromArgsArray(array &$array)
{
global $_TABLES, $_CONF;
/* magic_quotes_gpc cleanup routine now in submitstory() in
* /public_html/admin/story.php
*/
$retval = STORY_LOADED_OK; // default to success
// Load the trivial stuff:
$this->_loadBasics($array);
// override the GLText version to the latest version
$this->_text_version = GLTEXT_LATEST_VERSION;
/* Check to see if we have permission to edit this sid, and that this
* sid is not a duplicate or anything horrible like that. ewww.
*/
$sql = "SELECT owner_id, group_id, perm_owner, perm_group, perm_members, perm_anon FROM {$_TABLES['stories']} "
. " WHERE sid='" . DB_escapeString($this->_sid) . "' ";
$result = DB_query($sql);
if ($result && (DB_numRows($result) > 0)) {
//Sid exists! Is it our article?
if ($this->_sid != $this->_originalSid) {
// for story preview: don't abort
$retval = STORY_DUPLICATE_SID;
}
$article = DB_fetchArray($result);
// Check Security
if (SEC_hasAccess($article['owner_id'], $article['group_id'],
$article['perm_owner'], $article['perm_group'],
$article['perm_members'], $article['perm_anon']) < 3
) {
return STORY_EXISTING_NO_EDIT_PERMISSION;
}
}
$access = SEC_hasAccess(
$this->_owner_id, $this->_group_id,
$this->_perm_owner, $this->_perm_group,
$this->_perm_members, $this->_perm_anon
);
//if (($access < 3) || !SEC_hasTopicAccess($this->_tid) || !SEC_inGroup($this->_group_id)) {
if (($access < 3) || !TOPIC_hasMultiTopicAccess('topic') || !SEC_inGroup($this->_group_id)) {
return STORY_NO_ACCESS_PARAMS;
}
// Load up the topic name and icon
$topic = DB_query("SELECT tid, topic, imageurl FROM {$_TABLES['topics']} WHERE tid='" . TOPIC_getTopicDefault('topic') . "'");
$topic = DB_fetchArray($topic);
$this->_tid = $topic['tid'];
$this->_topic = $topic['topic'];
$this->_imageurl = $topic['imageurl'];
// Load the title, page title
$this->_title = $this->_applyTitleFilter($array['title']);
$this->_page_title = $this->_applyTitleFilter($array['page_title']);
// fix for bug in advanced editor
if (in_array($array['postmode'], array('html', 'adveditor', 'wikitext'))) {
if ($_CONF['advanced_editor'] && ($array['bodytext'] === '<br' . XHTML . '>')) {
$array['bodytext'] = '';
}
}
// Load the introtext, bodytext
$this->_introtext = $this->_applyTextFilter($array['introtext'], $array['postmode']);
$this->_bodytext = $this->_applyTextFilter($array['bodytext'], $array['postmode']);
$this->_advanced_editor_mode = 0;
if (in_array($array['postmode'], array('html', 'adveditor', 'wikitext'))) {
if ($this->_postmode === 'adveditor') {
$this->_advanced_editor_mode = 1;
$this->_postmode = 'html';
}
}
if (empty($this->_title) || empty($this->_introtext)) {
return STORY_EMPTY_REQUIRED_FIELDS;
}
$this->sanitizeData();
return $retval;
}
/**
* Sets up basic data for a new user submission story
*/
public function initSubmission()
{
global $_USER, $_CONF, $_TABLES, $topic;
if (COM_isAnonUser()) {
$this->_uid = 1;
} else {
$this->_uid = $_USER['uid'];
}
// initialize the GLText version to the latest version
$this->_text_version = GLTEXT_LATEST_VERSION;
$this->_postmode = $_CONF['postmode'];
// If a topic has been specified, use it, if permitted
// otherwise, fall back to the default permitted topic.
// if we still don't have one...
// Have we specified a permitted topic?
if (!empty($topic)) {
$allowed = DB_getItem($_TABLES['topics'], 'tid', "tid = '" . DB_escapeString($topic) . "'" . COM_getTopicSql('AND'));
if ($allowed != $topic) {
$topic = '';
}
}
// Do we now not have a topic?
if (empty($topic)) {
// Get default permitted:
$topic = DB_getItem($_TABLES['topics'], 'tid', 'is_default = 1' . COM_getPermSQL('AND'));
}
// Use what we have:
$this->_tid = $topic;
$this->_date = time();
}
/**
* Loads a submitted story from post data
*
* @return int
*/
public function loadSubmission()
{