fake_twitter /app/Controller/TwitsController.php

Language PHP Lines 255
MD5 Hash 5a23619e346e6ffd06c1d3d3a695f66a Estimated Cost $3,351 (why?)
Repository https://bitbucket.org/udeshika/fake_twitter View Raw File View Project SPDX
  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
<?php

/* $
  Document   : fktwitter
  Created on : Dec 26, 2011, 7:12:34 PM
  Author     : udeshika
  Description: fktwitter
 */

class TwitsController extends AppController {

    public $name = 'Twits';
    public $helpers = array('Html', 'Form', 'Js', 'Custom');
    public $components = array('Session');

    /**
     * get all twits everyone else X
     * @access public
     * @param 
     * @return posts array pass to the view
     */
    public function index() {
        if ($this->check_users()) {

            $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
            $this->set('posts', $results);
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * get all twits everyone else 
     * @access public
     * @param $user login user or all user profile $user name
     * @return posts array pass to the view, userdata array returns profile data
     */
    public function stream($user='') {

        if ($this->check_users()) {

            if ($user == 'everyone') {
                $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
            } else {
                $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username WHERE US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0  ORDER BY UNIX_TIMESTAMP(PO.created) DESC");

                $user_results = $this->Twit->query("SELECT US.*  FROM `users` as US WHERE US.username='" . $user . "'");
                $this->set('userdata', $user_results);
            }
            //$this->set('sidecontent_for_layout', 'sidecontent');
            $this->set('posts', $results);
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * get last insert twitt using AJAX
     * @access public
     * @param last insert id $id 
     * @return posts array pass to the view
     */
    public function getstream($id='') {

        if ($this->check_users()) {

            $this->layout = '';

            if ($id > 0) {
                $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username WHERE PO.id='" . $id . "'");
            } else {
                $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username ORDER BY PO.created DESC");
            }

            $this->set('posts', $results);
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * All status stream refreshed every 2 second using AJAX
     * @access public
     * @param select data acording to last twitt ($id) and username ($user)
     * @return posts array pass to the view
     */
    public function refreshe($id='', $user='') {

        if ($this->check_users()) {

            $this->layout = '';
            if ($user != '') {

                if ($id > 0) {
                    $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
            JOIN users as US ON US.username = PO.username WHERE PO.id >'" . $id . "' AND 
            (US.username='" . $user . "' OR LOCATE ('@" . $user . "', PO.twitt) > 0 ) ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
                    $this->set('posts', $results);
                }
            } else {
                if ($id > 0) {
                    $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
            JOIN users as US ON US.username = PO.username WHERE PO.username!='" . $this->Session->read('User') . "' AND PO.id >'" . $id . "'  ORDER BY UNIX_TIMESTAMP(PO.created) DESC");
                    $this->set('posts', $results);
                }
            }
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * more info twitt via jq ajax
     * @access public
     * @param int twitt $id
     * @return posts array pass to the view
     */
    public function view($id) {
        $this->layout = '';
        $results = $this->Twit->query("SELECT PO.* , US.username ,US.fname ,US.lname ,US.image FROM `twits` as PO LEFT
        JOIN users as US ON US.username = PO.username WHERE PO.id='".$id."'");
        $this->set('posts', $results);
    }

    /**
     * add new twitt via jq ajax
     * @access public
     * @param url convert to shorten and post will save
     * @return redirect twitt strim
     */
    public function ajaxadd() {
        if ($this->request->is('post')) {
            $this->layout = '';
            $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);
            if ($this->Twit->save($this->request->data)) {
                echo $this->Twit->getLastInsertId();
                // echo 'Your post has been saved.';
            } else {
                echo 'Unable to add your post.';
            }
        }
    }

    /**
     * add new twitt
     * @access public
     * @param url convert to shorten and post will save
     * @return redirect twitt strim
     */
    public function add() {
        if ($this->check_users()) {
            if ($this->request->is('post')) {

                $this->request->data['twitt'] = $this->set_url($this->request->data['twitt']);

                if ($this->Twit->save($this->request->data)) {
                    $this->Session->setFlash('Your post has been saved.');
                      $this->redirect('stream/everyone');
                } else {
                    echo 'Unable to add your post.';
                }
            }
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * delete twitter
     * @access public
     * @param delete twitt acording to $id
     * @return redirect to user profile
     */
    function delete($id) {
        if ($this->check_users()) {
            if ($this->request->is('get')) {
                throw new MethodNotAllowedException();
            }
            if ($this->Twit->delete($id)) {
                $this->Session->setFlash('The twitter with id: ' . $id . ' has been deleted.');
                $this->redirect(array('action' => 'stream/' . $this->Session->read('User') . ''));
            }
        } else {
            $this->redirect('../users/login');
        }
    }

    /**
     * string replase with <a href= and shortener url
     * @access public
     * @param string $text replase
     * @return text 
     */
    public function set_url($text) {

        $urlset = $this->geturls($text);

        foreach ($urlset[0] as $k => $url) {

            $url_shortener = $this->random_alpha(5);
            $text = str_replace($url, '<a href="' . $this->base . '/t/url/' . $url_shortener . '">' . $url . '</a>', $text);
            $this->insert_url($url, $url_shortener);
        }
        return $text;
    }

    /**
     * preg_match_all urls
     * @access public
     * @param string $text to $matches array
     * @return array 
     */
    public function geturls($text) {

        $matches = array();
        preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $text, $matches);
        return $matches;
    }

    /**
     * Inserts a URL to the database and returns the whole dataset
     * @access public
     * @param string $url Url to shorten
     * @return array Array with all available data inside, or empty one
     */
    function insert_url($url, $shortener_url) {
        $ret = array();
        $sql = 'INSERT INTO url_redirect (url,shortener_url)
						VALUES (
						\'' . $url . '\',
						\'' . $shortener_url . '\');';

        $results = $this->Twit->query($sql);
    }

    /**
     * randum 5 letter str
     * @access public
     * @param $alphNums all alph numeric values,$length length of new url 
     * @return pass new shorten url 
     */
    function random_alpha($length) {

        $alphNums = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $newString = str_shuffle(str_repeat($alphNums, rand(1, $length)));

        return substr($newString, rand(0, strlen($newString) - $length), $length);
    }

}
Back to Top