Code cleanup
[friendica.git/.git] / mod / crepair.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
6 use Friendica\Object\Contact;
7
8 require_once 'include/contact_selectors.php';
9 require_once 'mod/contacts.php';
10
11 function crepair_init(App $a) {
12         if (! local_user()) {
13                 return;
14         }
15
16         $contact_id = 0;
17
18         if(($a->argc == 2) && intval($a->argv[1])) {
19                 $contact_id = intval($a->argv[1]);
20                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
21                         intval(local_user()),
22                         intval($contact_id)
23                 );
24                 if (! DBM::is_result($r)) {
25                         $contact_id = 0;
26                 }
27         }
28
29         if(! x($a->page,'aside'))
30                 $a->page['aside'] = '';
31
32         if($contact_id) {
33                 $a->data['contact'] = $r[0];
34                 $contact = $r[0];
35                 profile_load($a, "", 0, Contact::getDetailsByURL($contact["url"]));
36         }
37 }
38
39 function crepair_post(App $a) {
40         if (! local_user()) {
41                 return;
42         }
43
44         $cid = (($a->argc > 1) ? intval($a->argv[1]) : 0);
45
46         if($cid) {
47                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
48                         intval($cid),
49                         intval(local_user())
50                 );
51         }
52
53         if (! DBM::is_result($r)) {
54                 return;
55         }
56
57         $contact = $r[0];
58
59         $name    = ((x($_POST,'name')) ? $_POST['name'] : $contact['name']);
60         $nick    = ((x($_POST,'nick')) ? $_POST['nick'] : '');
61         $url     = ((x($_POST,'url')) ? $_POST['url'] : '');
62         $request = ((x($_POST,'request')) ? $_POST['request'] : '');
63         $confirm = ((x($_POST,'confirm')) ? $_POST['confirm'] : '');
64         $notify  = ((x($_POST,'notify')) ? $_POST['notify'] : '');
65         $poll    = ((x($_POST,'poll')) ? $_POST['poll'] : '');
66         $attag   = ((x($_POST,'attag')) ? $_POST['attag'] : '');
67         $photo   = ((x($_POST,'photo')) ? $_POST['photo'] : '');
68         $remote_self = ((x($_POST,'remote_self')) ? $_POST['remote_self'] : false);
69         $nurl    = normalise_link($url);
70
71         $r = q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `nurl` = '%s', `request` = '%s', `confirm` = '%s', `notify` = '%s', `poll` = '%s', `attag` = '%s' , `remote_self` = %d
72                 WHERE `id` = %d AND `uid` = %d",
73                 dbesc($name),
74                 dbesc($nick),
75                 dbesc($url),
76                 dbesc($nurl),
77                 dbesc($request),
78                 dbesc($confirm),
79                 dbesc($notify),
80                 dbesc($poll),
81                 dbesc($attag),
82                 intval($remote_self),
83                 intval($contact['id']),
84                 local_user()
85         );
86
87         if($photo) {
88                 logger('mod-crepair: updating photo from ' . $photo);
89                 require_once("include/Photo.php");
90
91                 update_contact_avatar($photo,local_user(),$contact['id']);
92         }
93
94         if($r)
95                 info( t('Contact settings applied.') . EOL);
96         else
97                 notice( t('Contact update failed.') . EOL);
98
99
100         return;
101 }
102
103
104
105 function crepair_content(App $a) {
106
107         if (! local_user()) {
108                 notice( t('Permission denied.') . EOL);
109                 return;
110         }
111
112         $cid = (($a->argc > 1) ? intval($a->argv[1]) : 0);
113
114         if($cid) {
115                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
116                         intval($cid),
117                         intval(local_user())
118                 );
119         }
120
121         if (! DBM::is_result($r)) {
122                 notice( t('Contact not found.') . EOL);
123                 return;
124         }
125
126         $contact = $r[0];
127
128         $warning = t('<strong>WARNING: This is highly advanced</strong> and if you enter incorrect information your communications with this contact may stop working.');
129         $info = t('Please use your browser \'Back\' button <strong>now</strong> if you are uncertain what to do on this page.');
130
131         $returnaddr = "contacts/$cid";
132
133         $allow_remote_self = Config::get('system','allow_users_remote_self');
134
135         // Disable remote self for everything except feeds.
136         // There is an issue when you repeat an item from maybe twitter and you got comments from friendica and twitter
137         // Problem is, you couldn't reply to both networks.
138         if (!in_array($contact['network'], array(NETWORK_FEED, NETWORK_DFRN, NETWORK_DIASPORA)))
139                 $allow_remote_self = false;
140
141         if ($contact['network'] == NETWORK_FEED)
142                 $remote_self_options = array('0'=>t('No mirroring'), '1'=>t('Mirror as forwarded posting'), '2'=>t('Mirror as my own posting'));
143         else
144                 $remote_self_options = array('0'=>t('No mirroring'), '2'=>t('Mirror as my own posting'));
145
146         $update_profile = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DSPR, NETWORK_OSTATUS));
147
148         $tab_str = contacts_tab($a, $contact['id'], 5);
149
150
151         $tpl = get_markup_template('crepair.tpl');
152         $o .= replace_macros($tpl, array(
153                 //'$title'      => t('Repair Contact Settings'),
154                 '$tab_str'      => $tab_str,
155                 '$warning'      => $warning,
156                 '$info'         => $info,
157                 '$returnaddr'   => $returnaddr,
158                 '$return'       => t('Return to contact editor'),
159                 '$update_profile' => update_profile,
160                 '$udprofilenow' => t('Refetch contact data'),
161                 '$contact_id'   => $contact['id'],
162                 '$lbl_submit'   => t('Submit'),
163
164                 '$label_remote_self' => t('Remote Self'),
165                 '$allow_remote_self' => $allow_remote_self,
166                 '$remote_self' => array('remote_self',
167                                         t('Mirror postings from this contact'),
168                                         $contact['remote_self'],
169                                         t('Mark this contact as remote_self, this will cause friendica to repost new entries from this contact.'),
170                                         $remote_self_options
171                                 ),
172
173                 '$name'         => array('name', t('Name') , htmlentities($contact['name'])),
174                 '$nick'         => array('nick', t('Account Nickname'), htmlentities($contact['nick'])),
175                 '$attag'        => array('attag', t('@Tagname - overrides Name/Nickname'), $contact['attag']),
176                 '$url'          => array('url', t('Account URL'), $contact['url']),
177                 '$request'      => array('request', t('Friend Request URL'), $contact['request']),
178                 'confirm'       => array('confirm', t('Friend Confirm URL'), $contact['confirm']),
179                 'notify'        => array('notify', t('Notification Endpoint URL'), $contact['notify']),
180                 'poll'          => array('poll', t('Poll/Feed URL'), $contact['poll']),
181                 'photo'         => array('photo', t('New photo from this URL'), ''),
182         ));
183
184         return $o;
185
186 }