| Conditions | 13 |
| Paths | 84 |
| Total Lines | 85 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | function fire_vhost_cron($vhost) |
||
| 52 | { |
||
| 53 | global $VCRON; |
||
| 54 | |||
| 55 | if ($VCRON->TRACE_ENABLE) { |
||
| 56 | $CRONTRACE = fopen($VCRON->TRACE, 'a'); |
||
| 57 | } |
||
| 58 | $ch = curl_init($vhost->root_web.'/main/cron/run.php'); |
||
| 59 | |||
| 60 | $http_proxy_host = api_get_setting('vchamilo_httpproxyhost', 'vchamilo'); |
||
| 61 | $http_proxy_port = api_get_setting('vchamilo_httpproxyport', 'vchamilo'); |
||
| 62 | $http_proxy_bypass = api_get_setting('vchamilo_httpproxybypass', 'vchamilo'); |
||
| 63 | $http_proxy_user = api_get_setting('vchamilo_httpproxyuser', 'vchamilo'); |
||
| 64 | $http_proxy_password = api_get_setting('vchamilo_httpproxypassword', 'vchamilo'); |
||
| 65 | |||
| 66 | curl_setopt($ch, CURLOPT_TIMEOUT, $VCRON->TIMEOUT); |
||
| 67 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 68 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 69 | curl_setopt($ch, CURLOPT_USERAGENT, 'Chamilo'); |
||
| 70 | curl_setopt($ch, CURLOPT_POSTFIELDS, ''); |
||
| 71 | curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml charset=UTF-8"]); |
||
| 72 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
| 73 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
||
| 74 | |||
| 75 | // Check for proxy. |
||
| 76 | if (!empty($http_proxy_host) && !is_proxybypass($uri)) { |
||
| 77 | curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false); |
||
| 78 | |||
| 79 | if (empty($http_proxy_port)) { |
||
| 80 | echo "Using proxy $http_proxy_host\n"; |
||
| 81 | curl_setopt($ch, CURLOPT_PROXY, $http_proxy_host); |
||
| 82 | } else { |
||
| 83 | echo "Using proxy $http_proxy_host:$http_proxy_port\n"; |
||
| 84 | curl_setopt($ch, CURLOPT_PROXY, $http_proxy_host.':'.$http_proxy_port); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!empty($http_proxy_user) and !empty($http_proxy_password)) { |
||
| 88 | curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxy_user.':'.$http_proxy_password); |
||
| 89 | if (defined('CURLOPT_PROXYAUTH')) { |
||
| 90 | // any proxy authentication if PHP 5.1 |
||
| 91 | curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $timestamp_send = time(); |
||
| 97 | $rawresponse = curl_exec($ch); |
||
| 98 | $timestamp_receive = time(); |
||
| 99 | |||
| 100 | if ($rawresponse === false) { |
||
| 101 | $error = curl_errno($ch).':'.curl_error($ch); |
||
| 102 | if ($VCRON->TRACE_ENABLE) { |
||
| 103 | if ($CRONTRACE) { |
||
| 104 | fputs($CRONTRACE, "VCron start on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n"); |
||
| 105 | fputs($CRONTRACE, "VCron Error : $error \n"); |
||
| 106 | fputs($CRONTRACE, "VCron stop on $vhost->root_web : $timestamp_receive\n#################\n\n"); |
||
| 107 | fclose($CRONTRACE); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | echo "VCron started on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n"; |
||
| 111 | echo "VCron Error : $error \n"; |
||
| 112 | echo "VCron stop on $vhost->root_web : ".api_time_to_hms($timestamp_receive)."\n#################\n\n"; |
||
| 113 | |||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($VCRON->TRACE_ENABLE) { |
||
| 118 | if ($CRONTRACE) { |
||
| 119 | fputs($CRONTRACE, "VCron start on $vhost->vhostname : ".api_time_to_hms($timestamp_send)."\n"); |
||
| 120 | fputs($CRONTRACE, $rawresponse."\n"); |
||
| 121 | fputs($CRONTRACE, "VCron stop on $vhost->vhostname : ".api_time_to_hms($timestamp_receive)."\n#################\n\n"); |
||
| 122 | fclose($CRONTRACE); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | echo "VCron start on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n"; |
||
| 126 | echo $rawresponse."\n"; |
||
| 127 | echo "VCron stop on $vhost->root_web : ".api_time_to_hms($timestamp_receive)."\n#################\n\n"; |
||
| 128 | $vhost->lastcrongap = time() - $vhost->lastcron; |
||
| 129 | $vhost->lastcron = $timestamp_send; |
||
| 130 | $vhost->croncount++; |
||
| 131 | |||
| 132 | $vhostid = $vhost->id; |
||
| 133 | unset($vhost->id); |
||
| 134 | |||
| 135 | Database::update('vchamilo', (array) $vhost, ['id = ?' => $vhostid]); |
||
| 136 | } |
||
| 295 |