| 581 | | /** |
| 582 | | * Update Plugins |
| 583 | | * |
| 584 | | * @param <type> $h |
| 585 | | */ |
| 586 | | public function update($h) |
| 587 | | { |
| 588 | | $url = "http://hotaruplugins.com/zip/"; |
| 589 | | $folder = $h->plugin->folder; |
| 590 | | $version = $h->cage->get->getHtmLawed('version'); |
| 591 | | $findfolder = str_replace('_', '-', $folder); |
| 592 | | $version = str_replace('.', '-', $version); |
| 593 | | $copydir = PLUGINS.$folder."/"; |
| 594 | | $file = $findfolder."-".$version.".zip"; |
| 595 | | |
| 596 | | // get ftpsettings |
| 597 | | $ftpserver = ""; |
| 598 | | $ftppath = "/public_html/content/"; |
| 599 | | $ftpuser = ""; |
| 600 | | $ftppass = ""; |
| 601 | | |
| 602 | | |
| 603 | | $ftp_url = "ftp://".$ftpuser.":".stripslashes($ftppass)."@".$ftpserver.$ftppath.$folder."/"; |
| 604 | | |
| 605 | | // check that we can access the remote plugin repo site via curl |
| 606 | | if ($this->fileCheckCurlConnection($url, $file) == 200) { |
| 607 | | if ($write = is_writeable($copydir)) { |
| 608 | | //print "we can use PHP<br/>"; |
| 609 | | $this->filePhpWrite($h, $url, $file, $findfolder, $copydir); |
| 610 | | } else { |
| 611 | | //print "we will use FTP<br/>"; |
| 612 | | $this->fileFtpWrite($h, $url, $ftp_url, $file, $findfolder, $copydir); |
| 613 | | } |
| 614 | | } else { |
| 615 | | $h->messages[$h->lang['admin_theme_filecopy_permission_error']] = 'red'; |
| 616 | | //$h->messages[$file . $h->lang['admin_theme_fileexist_error']] = 'red'; |
| 617 | | } |
| 618 | | |
| 619 | | // unzip |
| 620 | | //print "checking if file exists " . PLUGINS . $folder . '/' . $file . '<br/>'; |
| 621 | | if (file_exists(PLUGINS.$folder.'/'.$file)) { |
| 622 | | //print "starting the unzip<br/>"; |
| 623 | | if (!$write) { |
| 624 | | $this->fileFtpChmod($h, $ftp_url, $folder, '777'); |
| 625 | | } |
| 626 | | /* |
| 627 | | * @todo Should we rename old files first and then bring in new ? |
| 628 | | */ |
| 629 | | |
| 630 | | $this->fileUnzip($h, $file, $copydir); |
| 631 | | |
| 632 | | if (!$write) { |
| 633 | | $this->fileFtpChmod($h, $ftp_url, $folder, '755'); |
| 634 | | } |
| 635 | | // delete zip file |
| 636 | | if ($write) { |
| 637 | | //print "we can use PHP<br/>"; |
| 638 | | $this->filePhpDelete($h, $file, $copydir); |
| 639 | | } else { |
| 640 | | //print "we will use FTP<br/>"; |
| 641 | | $this->fileFtpDelete($h, $ftp_url, $file, $copydir); |
| 642 | | } |
| 643 | | } else { |
| 644 | | $h->messages[$h->lang['admin_theme_filecopy_error'].$file] = 'red'; |
| 645 | | } |
| 646 | | } |
| 647 | | |
| 648 | | public function fileCheckCurlConnection($url, $file) |
| 649 | | { |
| 650 | | // create a new CURL resource |
| 651 | | $ch = curl_init(); |
| 652 | | // set URL and other appropriate options |
| 653 | | curl_setopt_array($ch, array( |
| 654 | | CURLOPT_URL => $url.$file, |
| 655 | | CURLOPT_HEADER => FALSE, |
| 656 | | CURLOPT_BINARYTRANSFER => TRUE, |
| 657 | | CURLOPT_RETURNTRANSFER => TRUE, |
| 658 | | CURLOPT_TIMEOUT => 30, // timeout after 30 seconds |
| 659 | | CURLOPT_NOBODY => TRUE, // We just need to check the connection |
| 660 | | )); |
| 661 | | // PHP should also timeout after 30 seconds |
| 662 | | set_time_limit(30); |
| 663 | | |
| 664 | | $zipfile = curl_exec($ch); |
| 665 | | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 666 | | |
| 667 | | curl_close($ch); |
| 668 | | |
| 669 | | return $statusCode; |
| 670 | | } |
| 671 | | |
| 672 | | public function filePhpWrite($h, $url, $file, $findfolder, $copydir) |
| 673 | | { |
| 674 | | // create a new CURL resource |
| 675 | | $ch = curl_init(); |
| 676 | | // set URL and other appropriate options |
| 677 | | curl_setopt_array($ch, array( |
| 678 | | CURLOPT_URL => $url.$file, |
| 679 | | CURLOPT_HEADER => FALSE, |
| 680 | | CURLOPT_BINARYTRANSFER => TRUE, |
| 681 | | CURLOPT_RETURNTRANSFER => TRUE, |
| 682 | | CURLOPT_TIMEOUT => 30, // timeout after 30 seconds |
| 683 | | CURLOPT_NOBODY => TRUE, // We just need to check the connection |
| 684 | | )); |
| 685 | | // PHP should also timeout after 30 seconds |
| 686 | | set_time_limit(30); |
| 687 | | |
| 688 | | $zipfile = curl_exec($ch); |
| 689 | | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 690 | | |
| 691 | | if ($statusCode == 200) { |
| 692 | | |
| 693 | | if (is_writeable($copydir)) { |
| 694 | | //reset this from above |
| 695 | | curl_setopt($ch, CURLOPT_NOBODY, false); |
| 696 | | $outfile = @fopen($copydir.$file, 'wb'); |
| 697 | | curl_setopt($ch, CURLOPT_FILE, $outfile); |
| 698 | | $handle = base64_encode(curl_exec($ch)); |
| 699 | | fclose($outfile); |
| 700 | | if ($handle) { |
| 701 | | $h->messages[$file.$h->lang['admin_theme_filecopy_success']] = 'green'; |
| 702 | | } |
| 703 | | } else { |
| 704 | | $h->messages[$h->lang['admin_theme_filecopy_error'].$file] = 'red'; |
| 705 | | } |
| 706 | | } else { |
| 707 | | $h->messages[$h->lang['admin_theme_fileexist_error'].$file] = 'red'; |
| 708 | | } |
| 709 | | curl_close($ch); |
| 710 | | } |
| 711 | | |
| 712 | | public function fileUnzip($h, $file, $copydir) |
| 713 | | { |
| 714 | | $h->messages[$file.$h->lang['admin_theme_filecopy_success']] = 'green'; |
| 715 | | |
| 716 | | $done = FALSE; |
| 717 | | if (extension_loaded('zip')) { |
| 718 | | $archive = new ZipArchive(); |
| 719 | | if ($done = ($archive->open($copydir.$file) === TRUE)) { |
| 720 | | $done = $archive->extractTo(PLUGINS); |
| 721 | | } |
| 722 | | } else { |
| 723 | | require_once(EXTENSIONS.'pclZip/pclzip.lib.php'); |
| 724 | | $archive = new PclZip($copydir.$file); |
| 725 | | $done = ($archive->extract(PCLZIP_OPT_PATH, PLUGINS) == 0); |
| 726 | | } |
| 727 | | |
| 728 | | if ($done) { |
| 729 | | $h->messages[$file.$h->lang['admin_theme_unzip_success']] = 'green'; |
| 730 | | } else { |
| 731 | | $h->messages[$h->lang['admin_theme_unzip_error'].$file] = 'red'; |
| 732 | | } |
| 733 | | } |
| 734 | | |
| 735 | | public function filePhpDelete($h, $file, $copydir) |
| 736 | | { |
| 737 | | @chmod($copydir.$file, 666); |
| 738 | | $deleted = @unlink($copydir.$file); |
| 739 | | if (!$deleted) { |
| 740 | | $h->messages[$file.$h->lang['admin_theme_zipdelete_error']] = 'yellow'; |
| 741 | | } |
| 742 | | } |
| 743 | | |
| 744 | | public function fileFtpChmod($h, $ftp_url, $folder, $permission) |
| 745 | | {// print "start chmod for " . $ftp_url; |
| 746 | | $ch = curl_init(); |
| 747 | | curl_setopt_array($ch, array( |
| 748 | | CURLOPT_URL => $ftp_url, |
| 749 | | CURLOPT_RETURNTRANSFER => false, |
| 750 | | CURLOPT_POSTQUOTE => array("CHMOD ".$permission.' '.$folder) |
| 751 | | )); |
| 752 | | |
| 753 | | curl_exec($ch); |
| 754 | | if ($error = curl_error($ch)) { |
| 755 | | Debug::log('curl', 'PluginManagement::fileFtpChmod() says: '.$error); |
| 756 | | } |
| 757 | | |
| 758 | | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 759 | | |
| 760 | | // 250 comes from FTP error code saying action completed ok |
| 761 | | if (!$statusCode == 250) { |
| 762 | | Debug::log('curl', 'PluginManagement::fileFtpChmod() says: FTP returned '.$statusCode); |
| 763 | | } |
| 764 | | |
| 765 | | curl_close($ch); |
| 766 | | } |
| 767 | | |
| 768 | | public function fileFtpDelete($h, $ftp_url, $file, $copydir) |
| 769 | | { |
| 770 | | $ch = curl_init(); |
| 771 | | curl_setopt_array($ch, array( |
| 772 | | CURLOPT_URL => $ftp_url.'plugins/', |
| 773 | | CURLOPT_RETURNTRANSFER => true, |
| 774 | | CURLOPT_POSTQUOTE => array("DELE ".$file) |
| 775 | | )); |
| 776 | | curl_exec($ch); |
| 777 | | |
| 778 | | $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 779 | | if (!$statusCode == 250) { // 250 comes from FTP error code saying action completed ok |
| 780 | | $h->messages[$file.$h->lang['admin_theme_zipdelete_error']] = 'yellow'; |
| 781 | | } |
| 782 | | |
| 783 | | curl_close($ch); |
| 784 | | } |
| 785 | | |
| 786 | | public function fileFtpWrite($h, $url, $ftp_url, $file, $folder, $copydir) |
| 787 | | { |
| 788 | | $ch = curl_init(); |
| 789 | | curl_setopt_array($ch, array( |
| 790 | | CURLOPT_URL => $url.$file, |
| 791 | | CURLOPT_WRITEFUNCTION => array($this, 'read_body'), // Set callback function for body |
| 792 | | CURLOPT_CONNECTTIMEOUT => 10, |
| 793 | | )); |
| 794 | | |
| 795 | | // Checking FTP at $url to get file $file |
| 796 | | |
| 797 | | curl_exec($ch); |
| 798 | | |
| 799 | | if ($error = curl_error($ch)) { |
| 800 | | $h->messages[$h->lang['admin_theme_filecopy_permission_error']] = 'red'; |
| 801 | | echo "Error: $error<br />\n"; |
| 802 | | } |
| 803 | | |
| 804 | | // Trying to upload to $ftp_url/plugins/$file; |
| 805 | | |
| 806 | | curl_setopt_array($ch, array( |
| 807 | | CURLOPT_URL => $ftp_url.'plugins/'.$file, |
| 808 | | CURLOPT_UPLOAD => 1, |
| 809 | | //CURLOPT_INFILE => 0, |
| 810 | | CURLOPT_CONNECTTIMEOUT => 5, |
| 811 | | //CURLOPT_VERBOSE => 1, |
| 812 | | CURLOPT_READFUNCTION => array($this, 'write_function'), |
| 813 | | //CURLOPT_INFILESIZE => filesize($localfile) |
| 814 | | )); |
| 815 | | |
| 816 | | curl_exec($ch); |
| 817 | | |
| 818 | | if ($error = curl_error($ch)) { |
| 819 | | $h->messages[$h->lang['admin_theme_filecopy_permission_error']] = 'red'; |
| 820 | | Debug::log('curl', 'PluginManagement::fileFtpWrite() says: '.$error); |
| 821 | | } |
| 822 | | |
| 823 | | curl_close($ch); |
| 824 | | |
| 825 | | return $error; |
| 826 | | } |
| 827 | | |
| 828 | | public function versionCheck($h) |
| 829 | | { |
| 830 | | $systeminfo = New SystemInfo(); |
| 831 | | $result = $systeminfo->plugin_version_getAll($h); |
| 832 | | if ($result) { |
| 833 | | $h->messages[$h->lang['admin_theme_version_check_completed']] = 'green'; |
| 834 | | } |
| 835 | | } |