· 6 years ago · Apr 30, 2019, 06:28 AM
1<?php
2namespace Plugins\AutoFollow;
3const IDNAME = "auto-follow";
4
5// Disable direct access
6if (!defined('APP_VERSION'))
7 die("Yo, what's up?");
8
9
10/**
11 * Event: plugin.install
12 */
13function install($Plugin)
14{
15 if ($Plugin->get("idname") != IDNAME) {
16 return false;
17 }
18
19 $sql = "CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."auto_follow_schedule` (
20 `id` INT NOT NULL AUTO_INCREMENT ,
21 `user_id` INT NOT NULL ,
22 `account_id` INT NOT NULL ,
23 `target` TEXT NOT NULL ,
24 `speed` VARCHAR(20) NOT NULL ,
25 `daily_pause` BOOLEAN NOT NULL,
26 `daily_pause_from` TIME NOT NULL,
27 `daily_pause_to` TIME NOT NULL,
28 `is_active` BOOLEAN NOT NULL ,
29 `schedule_date` DATETIME NOT NULL ,
30 `end_date` DATETIME NOT NULL ,
31 `last_action_date` DATETIME NOT NULL ,
32 `data` TEXT NOT NULL,
33 PRIMARY KEY (`id`),
34 INDEX (`user_id`),
35 INDEX (`account_id`)
36 ) ENGINE = InnoDB;";
37
38 $sql .= "CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."auto_follow_log` (
39 `id` INT NOT NULL AUTO_INCREMENT ,
40 `user_id` INT NOT NULL ,
41 `account_id` INT NOT NULL ,
42 `status` VARCHAR(20) NOT NULL,
43 `followed_user_pk` VARCHAR(50) NOT NULL,
44 `data` TEXT NOT NULL ,
45 `date` DATETIME NOT NULL ,
46 PRIMARY KEY (`id`),
47 INDEX (`user_id`),
48 INDEX (`account_id`),
49 INDEX (`followed_user_pk`)
50 ) ENGINE = InnoDB;";
51
52 $sql .= "ALTER TABLE `".TABLE_PREFIX."auto_follow_schedule`
53 ADD CONSTRAINT `".uniqid("ibfk_")."` FOREIGN KEY (`user_id`)
54 REFERENCES `".TABLE_PREFIX."users`(`id`)
55 ON DELETE CASCADE ON UPDATE CASCADE;";
56
57 $sql .= "ALTER TABLE `".TABLE_PREFIX."auto_follow_schedule`
58 ADD CONSTRAINT `".uniqid("ibfk_")."` FOREIGN KEY (`account_id`)
59 REFERENCES `".TABLE_PREFIX."accounts`(`id`)
60 ON DELETE CASCADE ON UPDATE CASCADE;";
61
62 $sql .= "ALTER TABLE `".TABLE_PREFIX."auto_follow_log`
63 ADD CONSTRAINT `".uniqid("ibfk_")."` FOREIGN KEY (`user_id`)
64 REFERENCES `".TABLE_PREFIX."users`(`id`)
65 ON DELETE CASCADE ON UPDATE CASCADE;";
66
67 $sql .= "ALTER TABLE `".TABLE_PREFIX."auto_follow_log`
68 ADD CONSTRAINT `".uniqid("ibfk_")."` FOREIGN KEY (`account_id`)
69 REFERENCES `".TABLE_PREFIX."accounts`(`id`)
70 ON DELETE CASCADE ON UPDATE CASCADE;";
71
72 $pdo = \DB::pdo();
73 $stmt = $pdo->prepare($sql);
74 $stmt->execute();
75}
76\Event::bind("plugin.install", __NAMESPACE__ . '\install');
77
78
79
80/**
81 * Event: plugin.remove
82 */
83function uninstall($Plugin)
84{
85 if ($Plugin->get("idname") != IDNAME) {
86 return false;
87 }
88
89 // Remove plugin settings
90 $Settings = \Controller::model("GeneralData", "plugin-auto-follow-settings");
91 $Settings->remove();
92
93 // Remove plugin tables
94 $sql = "DROP TABLE `".TABLE_PREFIX."auto_follow_schedule`;";
95 $sql .= "DROP TABLE `".TABLE_PREFIX."auto_follow_log`;";
96
97 $pdo = \DB::pdo();
98 $stmt = $pdo->prepare($sql);
99 $stmt->execute();
100}
101\Event::bind("plugin.remove", __NAMESPACE__ . '\uninstall');
102
103
104/**
105 * Add module as a package options
106 * Only users with correct permission
107 * Will be able to use module
108 *
109 * @param array $package_modules An array of currently active
110 * modules of the package
111 */
112function add_module_option($package_modules)
113{
114 $config = include __DIR__."/config.php";
115 ?>
116 <div class="mt-15">
117 <label>
118 <input type="checkbox"
119 class="checkbox"
120 name="modules[]"
121 value="<?= IDNAME ?>"
122 <?= in_array(IDNAME, $package_modules) ? "checked" : "" ?>>
123 <span>
124 <span class="icon unchecked">
125 <span class="mdi mdi-check"></span>
126 </span>
127 <?= __('Auto Follow') ?>
128 </span>
129 </label>
130 </div>
131 <?php
132}
133\Event::bind("package.add_module_option", __NAMESPACE__ . '\add_module_option');
134
135
136
137
138/**
139 * Map routes
140 */
141function route_maps($global_variable_name)
142{
143 // Settings (admin only)
144 $GLOBALS[$global_variable_name]->map("GET|POST", "/e/".IDNAME."/settings/?", [
145 PLUGINS_PATH . "/". IDNAME ."/controllers/SettingsController.php",
146 __NAMESPACE__ . "\SettingsController"
147 ]);
148
149 // Index
150 $GLOBALS[$global_variable_name]->map("GET|POST", "/e/".IDNAME."/?", [
151 PLUGINS_PATH . "/". IDNAME ."/controllers/IndexController.php",
152 __NAMESPACE__ . "\IndexController"
153 ]);
154
155 // Index
156 $GLOBALS[$global_variable_name]->map("GET|POST", "/e/".IDNAME."/sort/[:action]?", [
157 PLUGINS_PATH . "/". IDNAME ."/controllers/IndexController.php",
158 __NAMESPACE__ . "\IndexController"
159 ]);
160
161 // Schedule
162 $GLOBALS[$global_variable_name]->map("GET|POST", "/e/".IDNAME."/[i:id]/?", [
163 PLUGINS_PATH . "/". IDNAME ."/controllers/ScheduleController.php",
164 __NAMESPACE__ . "\ScheduleController"
165 ]);
166
167 // Log
168 $GLOBALS[$global_variable_name]->map("GET|POST", "/e/".IDNAME."/[i:id]/log/?", [
169 PLUGINS_PATH . "/". IDNAME ."/controllers/LogController.php",
170 __NAMESPACE__ . "\LogController"
171 ]);
172}
173\Event::bind("router.map", __NAMESPACE__ . '\route_maps');
174
175
176
177/**
178 * Event: navigation.add_special_menu
179 */
180function navigation($Nav, $AuthUser)
181{
182 $idname = IDNAME;
183 include __DIR__."/views/fragments/navigation.fragment.php";
184}
185\Event::bind("navigation.add_special_menu", __NAMESPACE__ . '\navigation');
186
187
188
189/**
190 * Add cron task to follow new users
191 */
192function addCronTask()
193{
194 require_once __DIR__."/models/SchedulesModel.php";
195 require_once __DIR__."/models/LogModel.php";
196
197
198 // Get auto follow schedules
199 $Schedules = new SchedulesModel;
200 $Schedules->where("is_active", "=", 1)
201 ->where("schedule_date", "<=", date("Y-m-d H:i:s"))
202 ->where("end_date", ">=", date("Y-m-d H:i:s"))
203 ->orderBy("last_action_date", "ASC")
204 ->setPageSize(10) // required to prevent server overload
205 ->setPage(1)
206 ->fetchData();
207
208 if ($Schedules->getTotalCount() < 1) {
209 return false;
210 }
211
212 $settings = namespace\settings();
213 $default_speeds = [
214 "very_slow" => 1,
215 "slow" => 2,
216 "medium" => 3,
217 "fast" => 4,
218 "very_fast" => 5,
219 ];
220 $speeds = $settings->get("data.speeds");
221 if (empty($speeds)) {
222 $speeds = [];
223 } else {
224 $speeds = json_decode(json_encode($speeds), true);
225 }
226 $speeds = array_merge($default_speeds, $speeds);
227
228 $as = [__DIR__."/models/ScheduleModel.php", __NAMESPACE__."\ScheduleModel"];
229 foreach ($Schedules->getDataAs($as) as $sc) {
230 $Log = new LogModel;
231 $Account = \Controller::model("Account", $sc->get("account_id"));
232 $User = \Controller::model("User", $sc->get("user_id"));
233
234 // Calculate next schedule datetime...
235 if (isset($speeds[$sc->get("speed")]) && (int)$speeds[$sc->get("speed")] > 0) {
236 $speed = (int)$speeds[$sc->get("speed")];
237 $delta = round(3600/$speed);
238
239 if ($settings->get("data.random_delay")) {
240 $delay = rand(0, 300);
241 $delta += $delay;
242 }
243 } else {
244 $delta = rand(720, 7200);
245 }
246
247 $next_schedule = date("Y-m-d H:i:s", time() + $delta);
248 if ($sc->get("daily_pause")) {
249 $pause_from = date("Y-m-d")." ".$sc->get("daily_pause_from");
250 $pause_to = date("Y-m-d")." ".$sc->get("daily_pause_to");
251 if ($pause_to <= $pause_from) {
252 // next day
253 $pause_to = date("Y-m-d", time() + 86400)." ".$sc->get("daily_pause_to");
254 }
255
256 if ($next_schedule > $pause_to) {
257 // Today's pause interval is over
258 $pause_from = date("Y-m-d H:i:s", strtotime($pause_from) + 86400);
259 $pause_to = date("Y-m-d H:i:s", strtotime($pause_to) + 86400);
260 }
261
262 if ($next_schedule >= $pause_from && $next_schedule <= $pause_to) {
263 $next_schedule = $pause_to;
264 }
265 }
266 $sc->set("schedule_date", $next_schedule)
267 ->set("last_action_date", date("Y-m-d H:i:s"))
268 ->save();
269
270
271 // Set default values for the log...
272 $Log->set("user_id", $User->get("id"))
273 ->set("account_id", $Account->get("id"))
274 ->set("status", "error");
275
276
277 // Check account
278 if (!$Account->isAvailable() || $Account->get("login_required")) {
279 // Account is either removed (unexected, external factors)
280 // Or login required for this account
281 // Deactivate schedule
282 $sc->set("is_active", 0)->save();
283
284 // Log data
285 $Log->set("data.error.msg", "Activity has been stopped")
286 ->set("data.error.details", "Re-login is required for the account.")
287 ->save();
288 continue;
289 }
290
291 // Check user account
292 if (!$User->isAvailable() || !$User->get("is_active") || $User->isExpired()) {
293 // User is not valid
294 // Deactivate schedule
295 $sc->set("is_active", 0)->save();
296
297 // Log data
298 $Log->set("data.error.msg", "Activity has been stopped")
299 ->set("data.error.details", "User account is either disabled or expred.")
300 ->save();
301 continue;
302 }
303
304 if ($User->get("id") != $Account->get("user_id")) {
305 // Unexpected, data modified by external factors
306 // Deactivate schedule
307 $sc->set("is_active", 0)->save();
308 continue;
309 }
310
311 // Check targets
312 $targets = @json_decode($sc->get("target"));
313 if (!$targets) {
314 // Unexpected, data modified by external factors
315 // Deactivate schedule
316 $sc->set("is_active", 0)->save();
317 continue;
318 }
319
320 // Select random target
321 $i = rand(0, count($targets) - 1);
322 $target = $targets[$i];
323
324 // Check selected target
325 if (empty($target->type) ||
326 empty($target->id) ||
327 !in_array($target->type, ["hashtag", "location", "people"]))
328 {
329 // Unexpected, data modified by external factors
330 continue;
331 }
332
333 try {
334 $Instagram = \InstagramController::login($Account);
335 } catch (\Exception $e) {
336 // Couldn't login into the account
337 $Account->refresh();
338
339 // Log data
340 if ($Account->get("login_required")) {
341
342 $error_count = $sc->get("data.error_count");
343
344 if($error_count == null){
345 $error_count = 0;
346 }
347
348 $error_count++;
349 $sc->set("data.error_count", $error_count);
350 if($error_count >= 3){
351 $sc->set("is_active", 0)->save();
352 $sc->set("data.error_count", 0);
353 }
354 $Log->set("data.error.msg", "Activity Relogin/Connection Problems " . $error_count ."/3");
355 } else {
356 $Log->set("data.error.msg", "Action re-scheduled");
357 }
358
359 $Log->set("data.error.details", $e->getMessage())
360 ->save();
361
362 continue;
363 }
364
365 // Logged in successfully
366 // Now script will try to get feed and follow new user
367 // And will log result
368 $Log->set("data.trigger", $target);
369
370
371 // Find username to follow
372 $follow_pk = null;
373 $follow_username = null;
374 $follow_full_name = null;
375 $follow_profile_pic_url = null;
376
377 $turns = 1;
378
379 $rank_token = \InstagramAPI\Signatures::generateUUID();
380
381 if ($target->type == "hashtag") {
382 try {
383 // Find new user to follow with pagination - BEGIN
384 $maxId = null;
385 do {
386 $feed = $Instagram->hashtag->getFeed(
387 str_replace("#", "", $target->id), $rank_token, $maxId);
388 $maxId = $feed->getNextMaxId();
389 $items = $feed->getItems();
390
391 if (count($items) < 1) {
392 // Invalid
393 break;
394 }
395
396 foreach ($items as $item) {
397 if (empty($item->getUser()->getFriendshipStatus()->getFollowing()) &&
398 empty($item->getUser()->getFriendshipStatus()->getOutgoingRequest()) &&
399 $item->getUser()->getPk()!= $Account->get("instagram_id"))
400 {
401 $_log = new LogModel([
402 "user_id" => $User->get("id"),
403 "account_id" => $Account->get("id"),
404 "followed_user_pk" => $item->getUser()->getPk(),
405 "status" => "success"
406 ]);
407
408 if (!$_log->isAvailable()) {
409
410 $filter1 = namespace\customFilterFast($item->getUser()->getPk(),$item->getUser()->getFullName(),$item->getUser()->getProfilePicUrl(),(bool)$item->getUser()->getIsPrivate(),$sc,$User,$Account);
411
412 if(!$filter1){
413 continue;
414 }
415 $filter = namespace\customFilter($Instagram, $item->getUser()->getPk(), $sc, $User,$Account);
416
417 if (!$filter && $turns <= 3){
418 $turns++;
419 continue;
420 }
421
422 // Found new user
423 $follow_pk = $item->getUser()->getPk();
424 $follow_username = $item->getUser()->getUsername();
425 $follow_full_name = $item->getUser()->getFullName();
426 $follow_profile_pic_url = $item->getUser()->getProfilePicUrl();
427 $follow_is_private = (bool)$item->getUser()->getIsPrivate();
428 break;
429 }
430 }
431 }
432 if (empty($maxId)) {
433 break;
434 }
435 sleep(3);
436 } while (empty($follow_pk));
437 // Find new user to follow with pagination - END
438
439 } catch (\Exception $e) {
440 // Couldn't get instagram feed related to the hashtag
441
442 // Log data
443 $Log->set("data.error.msg", "Couldn't get the feed")
444 ->set("data.error.details", $e->getMessage())
445 ->save();
446 continue;
447 }
448
449 } else if ($target->type == "location") {
450 try {
451 // Find new user to follow with pagination - BEGIN
452 $maxId = null;
453 do {
454 $feed = $Instagram->location->getFeed($target->id, $rank_token, $maxId);
455 $maxId = $feed->getNextMaxId();
456 $items = $feed->getItems();
457
458 if ($feed->getStory()){
459 $items = array_merge($items, $feed->getStory()->getItems());
460 }
461
462 if ($feed->getRankedItems()){
463 $items = array_merge($items, $feed->getRankedItems());
464 }
465
466 if (count($items) < 1) {
467 // Invalid
468 break;
469 }
470
471 foreach ($items as $item) {
472 if (empty($item->getUser()->getFriendshipStatus()) &&
473 $item->getUser()->getPk() != $Account->get("instagram_id")) {
474 $_log = new LogModel([
475 "user_id" => $User->get("id"),
476 "account_id" => $Account->get("id"),
477 "followed_user_pk" => $item->getUser()->getPk(),
478 "status" => "success"
479 ]);
480
481 if (!$_log->isAvailable()) {
482
483 // Codingmatter Modification
484 // Lets get all user related data for filtering.
485 $filter1 = namespace\customFilterFast($item->getUser()->getPk(),$item->getUser()->getFullName(),$item->getUser()->getProfilePicUrl(),(bool)$item->getUser()->getIsPrivate(),$sc,$User,$Account);
486
487 if(!$filter1){
488 continue;
489 }
490
491 //filter
492 $filter = namespace\customFilter($Instagram, $item->getUser()->getPk(), $sc, $User,$Account);
493
494 if (!$filter && $turns <= 3){
495 $turns++;
496 continue;
497 }
498
499 // Found new user
500 $follow_pk = $item->getUser()->getPk();
501 $follow_username = $item->getUser()->getUsername();
502 $follow_full_name = $item->getUser()->getFullName();
503 $follow_profile_pic_url = $item->getUser()->getProfilePicUrl();
504 $follow_is_private = (bool)$item->getUser()->getIsPrivate();
505 break;
506 }
507 }
508 }
509 if (empty($maxId)) {
510 break;
511 }
512 sleep(3);
513 } while (empty($follow_pk));
514 // Find new user to follow with pagination - END
515
516 } catch (\Exception $e) {
517 // Couldn't get instagram feed related to the location id
518
519 // Log data
520 $Log->set("data.error.msg", "Couldn't get the feed")
521 ->set("data.error.details", $e->getMessage())
522 ->save();
523 continue;
524 }
525
526 } else if ($target->type == "people") {
527 try {
528 // Find new user to follow with pagination - BEGIN
529 $maxId = null;
530 do {
531 $feed = $Instagram->people->getFollowers($target->id, $rank_token, null, $maxId);
532 $maxId = $feed->getNextMaxId();
533 $items = $feed->getUsers();
534
535 if (count($items) < 1) {
536 // Invalid
537 break;
538 }
539
540 foreach ($items as $item) {
541 if (empty($item->getFriendshipStatus()) &&
542 $item->getPk() != $Account->get("instagram_id")) {
543 $_log = new LogModel([
544 "user_id" => $User->get("id"),
545 "account_id" => $Account->get("id"),
546 "followed_user_pk" => $item->getPk(),
547 "status" => "success"
548 ]);
549
550 if (!$_log->isAvailable()) {
551
552 $filter1 = namespace\customFilterFast($item->getPk(),$item->getFullName(),$item->getProfilePicUrl(),(bool)$item->getIsPrivate(),$sc,$User,$Account);
553
554 if(!$filter1){
555 continue;
556 }
557
558 $filter = namespace\customFilter($Instagram, $item->getPk(), $sc, $User,$Account);
559
560 if (!$filter && $turns <= 3){
561 $turns++;
562 continue;
563 }
564
565 // Found new user
566 $follow_pk = $item->getPk();
567 $follow_username = $item->getUsername();
568 $follow_full_name = $item->getFullName();
569 $follow_profile_pic_url = $item->getProfilePicUrl();
570 $follow_is_private = (bool)$item->getIsPrivate();
571 break;
572 }
573 }
574 }
575 if (empty($maxId)) {
576 break;
577 }
578 sleep(3);
579 } while (empty($follow_pk));
580 // Find new user to follow with pagination - END
581
582 } catch (\Exception $e) {
583 // Couldn't get instagram feed related to the people id
584
585 // Log data
586 $Log->set("data.error.msg", "Couldn't get the feed")
587 ->set("data.error.details", $e->getMessage())
588 ->save();
589 continue;
590 }
591 }
592
593 if (empty($follow_pk)) {
594 $Log->set("data.error.msg", "Couldn't find new user to follow")
595 ->save();
596 continue;
597 }
598
599
600 // New user found to follow
601 try {
602
603 // MODIFICATION by Codingmatters.
604 // https://www.facebook.com/CodingMatters-945340885622490/
605 // We want a Powerlike feature.
606 // pull the timeline of the getFollower
607 // and get last 3 posts to like.
608
609 $power_count = $sc->get("data.powerlike_count");
610 $power_like = $sc->get("data.powerlike");
611 $power_random = $sc->get("data.powerlike_random");
612 $likedmedia = [];
613
614 if($power_count > 3){
615 $power_count = 3; //max value
616 };
617
618 if($power_like && !$follow_is_private){
619 try {
620 $feed = $Instagram->timeline->getUserFeed($follow_pk);
621
622 $items = $feed->getItems();
623
624 if($power_random){
625 $power_count = mt_rand(1, intval($power_count));
626 }
627
628 $temp_count = $power_count;
629
630 foreach ($items as $item) {
631
632 $media = new \stdClass();
633
634 if (!empty($item->getId()) && !$item->getHasLiked()) {
635
636 if($temp_count== 0){
637 break;
638 }else{
639 $temp_count = $temp_count - 1;
640 }
641
642 $media->media_id = $item->getId();
643 $media->media_code = $item->getCode();
644 $media->media_type = $item->getMediaType();
645 $media->media_thumb = namespace\_get_media_thumb_igitem($item);
646 $media->user_pk = $item->getUser()->getPk();
647 $media->user_username = $item->getUser()->getUsername();
648 $media->user_full_name = $item->getUser()->getFullName();
649
650 try {
651 $resp = $Instagram->media->like($item->getId());
652 } catch (\Exception $e) {
653 continue;
654 }
655
656 if (!$resp->isOk()) {
657 continue;
658 }else{
659 array_push($likedmedia, $media);
660 }
661 }
662 }
663
664 } catch (\Exception $e) {
665 // Couldn't get instagram feed related to the hashtag
666 }
667
668 }else{
669 $power_count = 0;
670 }
671
672 $resp = $Instagram->people->follow($follow_pk);
673
674 } catch (\Exception $e) {
675 $msg = $e->getMessage();
676 $msg = explode(":", $msg, 2);
677 $msg = isset($msg[1]) ? $msg[1] : $msg[0];
678 $msg = trim($msg);
679 if ($msg == "Feedback required.") {
680 $Log->set("data.error.msg", "Instagram Blocking The Request")
681 ->set("data.error.details", "The next action will be in 4 hours for the account safety.")
682 ->save();
683 $next_schedule2 = date("Y-m-d H:i:s", time() + 14400);
684 $sc->set("schedule_date", $next_schedule2)
685 ->set("last_action_date", date("Y-m-d H:i:s"))
686 ->save();
687 } else {
688 $Log->set("data.error.msg", "Couldn't Follow The User")
689 ->set("data.error.details", $msg)
690 ->save();
691 }
692 continue;
693 }
694
695
696 if (!$resp->isOk()) {
697 $Log->set("data.error.msg", "Couldn't follow the user")
698 ->set("data.error.details", "Something went wrong")
699 ->save();
700 continue;
701 }
702
703
704 // Followed new user successfully
705 $Log->set("status", "success")
706 ->set("data.followed", [
707 "pk" => $follow_pk,
708 "username" => $follow_username,
709 "full_name" => $follow_full_name,
710 "profile_pic_url" => $follow_profile_pic_url
711 ])
712 ->set("data.powerlike", [
713 "count" => count($likedmedia),
714 "posts" => $likedmedia
715 ])
716 ->set("followed_user_pk", $follow_pk)
717 ->save();
718 }
719}
720\Event::bind("cron.add", __NAMESPACE__."\addCronTask");
721
722
723/**
724 * Get Plugin Settings
725 * @return \GeneralDataModel
726 */
727function settings()
728{
729 $settings = \Controller::model("GeneralData", "plugin-auto-follow-settings");
730 return $settings;
731}
732
733function _get_media_thumb_igitem($item)
734{
735 $media_thumb = null;
736
737 $media_type = empty($item->getMediaType()) ? null : $item->getMediaType();
738
739 if ($media_type == 1 || $media_type == 2) {
740 // Photo (1) OR Video (2)
741 $candidates = $item->getImageVersions2()->getCandidates();
742 if (!empty($candidates[0]->getUrl())) {
743 $media_thumb = $candidates[0]->getUrl();
744 }
745 } else if ($media_type == 8) {
746 // ALbum
747 $carousel = $item->getCarouselMedia();
748 $candidates = $carousel[0]->getImageVersions2()->getCandidates();
749 if (!empty($candidates[0]->getUrl())) {
750 $media_thumb = $candidates[0]->getUrl();
751 }
752 }
753
754
755
756 return $media_thumb;
757}
758
759function customFilterFast($user_pk,$fullname,$picture,$private,$sc,$User,$Account){
760
761
762 $filter_dont_follow_twice = (bool)$sc->get("data.filter_unfollowed");
763 $filter_gender = $sc->get("data.filter_gender");
764 $filter_profil_private = (bool)$sc->get("data.filter_privat");
765 $filter_profil_picture = (bool)$sc->get("data.filter_picture");
766
767 if($filter_dont_follow_twice){
768 $q4 = "SELECT * FROM ".TABLE_PREFIX."auto_unfollow_log WHERE user_id = " . intval($User->get("id")) . " AND account_id = " . intval($Account->get("id")) . " AND unfollowed_user_pk = " . intval($user_pk) . " AND status = 'success'";
769 $query = \DB::query($q4);
770 $logs = $query->get();
771
772 if (count($logs) > 0) {
773 //echo "<br> ## Filter - Follow twice Triggered <br>";
774 return false;
775 }
776 }
777
778 if($filter_gender == "male" || $filter_gender == "female"){
779 //$gender_check = namespace\Gender($userinfo->getUser()>getFullName(), $Account->get("proxy"));
780 $gender_check = namespace\Gender2($filter_gender, $fullname);
781 if(!$gender_check){
782 //echo "<br> ## Filter - Gender not match <br>";
783 return false;
784 }
785 //echo "<br> ++ Filter - Gender match <br>";
786 }
787
788 if($filter_profil_private){
789 if($private){
790 //echo "<br> ## Filter - Private Profil Triggered <br>";
791 return false;
792 }
793 }
794
795 if($filter_profil_picture){
796 if($picture == ""){
797 //echo "<br> ## Filter - Profil Picture Tiggered <br>";
798 return false;
799 }
800 }
801
802 return true;
803
804}
805
806
807function customFilter($Instagram,$user_pk,$sc,$User,$Account){
808
809 $validation = false;
810
811 $filter_profil_business = (bool)$sc->get("data.filter_business");
812
813 $filter_media_min = (int)$sc->get("data.filter_media_min");
814 $filter_follower_min = (int)$sc->get("data.filter_followed_min");
815 $filter_follower_max = (int)$sc->get("data.filter_followed_max");
816 $filter_following_min = (int)$sc->get("data.filter_following_min");
817 $filter_following_max = (int)$sc->get("data.filter_following_max");
818 $filter_blacklist = explode(",",$sc->get("data.filter_blacklist"));
819
820 $userinfo = $Instagram->people->getInfoById($user_pk);
821
822 if($userinfo->getUser()->getIsBusiness()){
823
824 if($userinfo->getUser()->getPublicEmail() != ""){
825 $info_mail = $userinfo->getUser()->getPublicEmail();
826 }else{$info_mail = "NO MAIL";}
827
828 if($userinfo->getUser()->getContactPhoneNumber()!= ""){
829 $info_phone = $userinfo->getUser()->getContactPhoneNumber();
830 }else{$info_phone = "NO NUMBER";}
831
832 if($userinfo->getUser()->getCityName() != ""){
833 $info_cname = $userinfo->getUser()->getCityName();
834 }else{$info_cname = "NO CITY";}
835
836 if($userinfo->getUser()->getAddressStreet() != ""){
837 $info_street = trim(preg_replace('/\s\s+/', ' ', $userinfo->getUser()->getAddressStreet()));
838 }else{$info_street = "NO STREET";}
839
840 if($userinfo->getUser()->getExternalUrl() != ""){
841 $info_eurl = $userinfo->getUser()->getExternalUrl();
842 }else{$info_eurl = "NO URL";}
843
844 if($userinfo->getUser()->getCategory() != ""){
845 $info_category = $userinfo->getUser()->getCategory();
846 }else{$info_category = "NO Category";}
847
848 if($userinfo->getUser()->getProfilePicUrl() != ""){
849 $info_ppic = $userinfo->getUser()->getProfilePicUrl();
850 }else{$info_ppic = "NO Picture";}
851
852 if($userinfo->getUser()->getBiography() != ""){
853 $info_bio = str_replace(";" , " ", $userinfo->getUser()->getBiography());
854 $info_bio = str_replace("\n", " ", $info_bio);
855 $info_bio = str_replace("\r", " ", $info_bio);
856 }else{$info_bio = "NO Biotext";}
857
858 }
859
860 if(count($filter_blacklist) > 0){
861 //echo "<br> ## Filter - Blacklist check <br>";
862 $tempString = $userinfo->getUser()->getUsername() . " ";
863 $tempString .= $userinfo->getUser()->getFullName() . " ";
864 $tempString .= $userinfo->getUser()->getBiography() . " ";
865
866 foreach ($filter_blacklist as $key => $value){
867 //echo "<br> ## Filter - Blacklist keyword : ".$value."<br>";
868 if (strpos($value, $tempString) !== false) {
869 //echo "<br> ## Filter - Blacklist keyword found : ".$value."<br>";
870 return $validation;
871 }
872
873 }
874
875 }
876
877 if($filter_profil_business){
878 if($userinfo->getUser()->getIsBusiness() == 1){
879 //echo "<br> ## Filter - Business Tiggered <br>";
880 return $validation;
881 }
882 }
883
884 // Media
885
886 if($filter_media_min > 0){
887 if($userinfo->getUser()->getMediaCount() <= $filter_media_min){
888 //echo "<br> ## Filter - Media count Tiggered <br>";
889 return $validation;
890 }
891 }
892
893 // Followers
894
895 if($filter_follower_max > 0){
896 if($userinfo->getUser()->getFollowerCount() >= $filter_follower_max){
897 //echo "<br> ## Filter - Followers Max <br>";
898 return $validation;
899 }
900 }
901
902 if($filter_follower_min > 0){
903 if($userinfo->getUser()->getFollowerCount() <= $filter_follower_min){
904 //echo "<br> ## Filter - Followers Min <br>";
905 return $validation;
906 }
907 }
908
909 //Followings
910
911 if($filter_following_max > 0){
912 if($userinfo->getUser()->getFollowingCount() >= $filter_following_max){
913 //echo "<br> ## Filter - Following Max <br>";
914 return $validation;
915 }
916 }
917
918 if($filter_following_min > 0){
919 if($userinfo->getUser()->getFollowingCount() <= $filter_following_min){
920 //echo "<br> ## Filter - Following Min <br>";
921 return $validation;
922 }
923 }
924
925 return true;
926}
927
928function Gender2($gender,$fullname){
929
930 if($fullname == ""){
931 return false;
932 }
933
934 $firstname = strtolower(explode(" ", $fullname)[0]);
935
936 $firstname = preg_replace('~[^a-zA-Z]+~', '', $firstname);
937
938 if(strlen($firstname) <= 2){
939 return false;
940 }
941
942
943 if($gender == "female"){
944 $names = file_get_contents(PLUGINS_PATH."/".IDNAME."/assets/female.json");
945
946 $array_names = json_decode($names, true);
947
948 if (in_array($firstname, array_map('strtolower', $array_names['female']))) {
949 return true;
950 }
951
952 }else{
953 $names = file_get_contents(PLUGINS_PATH."/".IDNAME."/assets/male.json");
954
955 $array_names = json_decode($names, true);
956
957 if (in_array($firstname, array_map('strtolower', $array_names['male']))) {
958 return true;
959 }
960
961 }
962
963 // lets save names we could not resolve and add them later
964 $file = PLUGINS_PATH."/".IDNAME."/assets/".$gender.'_fails.txt';
965 $myfile = file_put_contents($file, $firstname.PHP_EOL , FILE_APPEND | LOCK_EX);
966
967 return false;
968}
969
970function Gender($fullname,$proxy){
971 $app_url = "https://api.genderize.io/?name=";
972
973 //$loginpassw = 'username:password';
974 //$proxy_ip = '192.168.1.1';
975 //$proxy_port = '12345';
976 //$url = 'http://www.domain.com';
977 $proxy = "https://188.39.20.136:8080";
978
979 if($fullname != ""){
980 $loginpassw = false;
981
982 if($proxy != ""){
983 $foo = explode(":",$proxy);
984
985 if(count($foo) == 3){
986 //ohne password
987 $proxy_type = $foo[0];
988 $proxy_ip = $foo[0] .":". $foo[1];
989 $proxy_port = $foo[2];
990 $loginpassw = false;
991 //echo "<br>".$proxy_ip."<br>";
992 }
993
994 }
995 $url = $app_url . urldecode(explode(" ", $fullname)[0]);
996 //echo "<br>".$url."<br>";
997 try{
998 $curl = curl_init();
999 curl_setopt($curl, CURLOPT_URL, $url);
1000 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
1001 curl_setopt($curl, CURLOPT_HEADER, false);
1002
1003 if($proxy != ""){
1004 //curl_setopt($curl, CURLOPT_PROXYPORT, $proxy_port);
1005 //curl_setopt($curl, CURLOPT_PROXYTYPE, strtoupper($proxy_type));
1006 curl_setopt($curl, CURLOPT_PROXY, $proxy);
1007 if($loginpassw != false){
1008 curl_setopt($curl, CURLOPT_PROXYUSERPWD, $loginpassw);
1009 }
1010 }
1011 //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
1012 //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
1013 $data = curl_exec($curl);
1014
1015 if(curl_errno($curl))
1016 //echo 'Curl error: '.curl_error($curl);
1017
1018 curl_close($curl);
1019 //echo "<br> Gender Lookup Proxy Error <br>";
1020
1021 $data = json_decode($data);
1022 if(!empty($data) && isset($data->gender)){
1023 return $data->gender;
1024 }else{
1025 return false;
1026 }
1027 }catch(\Exception $e){
1028 //echo "<br> Gender Lookup Proxy Error <br>";
1029 return false;
1030 }
1031 }else{
1032 return false;
1033 }
1034}